Thursday 29 November 2012

read and write data into xml using gridview


//creation of gridview

<table width="900px">
                <tr>
                    <td style="background-color: Silver; width: 900">
                        <asp:GridView ID="gvClientTestimonials" runat="server" AutoGenerateColumns="false"
                            OnRowDataBound="gvClientTestimonials_RowDataBound">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <p>
                                            <img src="images/open-quotes.gif" width="19" height="12" hspace="5" vspace="5" align="bottom"><asp:Label
                                                ID="lblTestimonial" runat="server">
                                            </asp:Label>
                                            <img src="images/close-quotes1.gif" width="19" height="12" hspace="5" vspace="5"
                                                align="middle"></p>
                                        <div align="right">
                                            <strong>--<asp:Label ID="lblClientName" runat="server"></asp:Label></strong></div>
                                        <br />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>

//in.cs file


protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DataSet ds = new DataSet("Testimonials");
                //finding xml file is exist or not

                if (File.Exists(Server.MapPath("Testimonials.xml")) == false)
                {                  
                    ds.Tables.Add("Testimonials");
                    ds.Tables[0].Columns.Add("Id");
                    ds.Tables[0].Columns.Add("Testimonial");
                    ds.Tables[0].Columns.Add("Client");
                    ds.Tables[0].Columns.Add("DateTime");
                   // ds.Tables[0].Rows.Add(txtClientTestimonials.Text, txtClientName.Text, DateTime.Now.ToString());
                    ds.Tables[0].Rows.Add(0, "Working with the some is a great pleasure!", "ram", DateTime.Now.ToString());
                    ds.WriteXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
                    ds.Clear();
                    ds.ReadXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
                    gvClientTestimonials.DataSource = ds;
                    gvClientTestimonials.DataBind();
                }
                else
                {
                    ds.ReadXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
                    gvClientTestimonials.DataSource = ds;
                    gvClientTestimonials.DataBind();
                }
               
            }
           
        }
        protected void gvClientTestimonials_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblTestimonial = (Label)e.Row.FindControl("lblTestimonial");
                Label lblClientName = (Label)e.Row.FindControl("lblClientName");
                DataRowView drv = (DataRowView)e.Row.DataItem;
                lblTestimonial.Text = drv["Testimonial"].ToString();
                lblClientName.Text = drv["Client"].ToString();
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet("Testimonials");
            if (File.Exists(Server.MapPath("ClientTestimonials/Testimonials.xml")) == false)
            {
                ds.Tables.Add("Testimonials");
                ds.Tables[0].Columns.Add("Id");
                ds.Tables[0].Columns.Add("Testimonial");
                ds.Tables[0].Columns.Add("Client");
                ds.Tables[0].Columns.Add("DateTime");              
                ds.Tables[0].Rows.Add(0,"Working with the Roopasoft is a great pleasure!","James",DateTime.Now.ToString());
                ds.WriteXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
            }
            else
            {
                ds.ReadXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
                ds.Tables[0].Rows.Add(ds.Tables[0].Rows.Count+1,txtClientTestimonials.Text, txtClientName.Text, DateTime.Now.ToString());
                ds.WriteXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
            }
            gvClientTestimonials.DataSource = ds;
            gvClientTestimonials.DataBind();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtClientName.Text = "";
            txtClientTestimonials.Text = "";

        }

Sunday 18 November 2012

create binary image converstion and stores in database


//on button click(browse button)

private void btnProductVariantBrowse_Click(object sender, EventArgs e)
        {
  //it stores the file name in textbox
            txtProductImage.Text = BrowseFile();
         
        }

//it open window and return the file name

public string BrowseFile()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return ofd.FileName;
            }
            return "";
        }
//picture details


 if (File.Exists(txtProductImage.Text))
            {
                FileInfo fi = new FileInfo(txtProductImage.Text);
                pvDetails.picture = new Picture();
                pvDetails.picture.PictureBinary = DbHelper.FileToByteArray(txtProductImage.Text);
                pvDetails .picture .MimeType="Image/" + fi.Extension.Substring(1, fi.Extension.Length - 1);
                pvDetails .picture.IsNew =true;       
               

            }
           

//file to byte array class


 public static byte[] FileToByteArray(string _FileName)
        {
            byte[] bytArrData = null;
            try
            {
                // Open file for reading
                System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                // attach filestream to binary reader
                System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
                // get total byte length of the file
                long _TotalBytes = new System.IO.FileInfo(_FileName).Length;
                // read entire file into buffer
                bytArrData = _BinaryReader.ReadBytes((Int32)_TotalBytes);

                // close file reader
                _FileStream.Close();
                _FileStream.Dispose();
                _BinaryReader.Close();
            }
            catch (Exception ex)
            {
                // Error
                //Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
            }
            return bytArrData;
        }



web editor in windows form


        private void frmPrdouctVariant_Load(object sender, EventArgs e)
        {
            _objMdiMalta = (mdiAMalta)this.MdiParent;

            ClearProductDetails();
            wbFullDescription.Navigate(Path.GetDirectoryName(Application.ExecutablePath).Replace("\\bin\\Debug", string.Empty) + "\\tinymce\\examples\\HtmlEditor.html");

        }
        public string HtmlFullDescription
        {
            get
            {
                string content = string.Empty;
                if (wbFullDescription.Document != null)
                {
                    object html = wbFullDescription.Document.InvokeScript("GetContent");
                    content = html as string;
                }
                return content;
            }
            set
            {
                if (wbFullDescription.Document != null)
                {
                    wbFullDescription.Document.InvokeScript("SetContent", new object[] { value });
                    // wbFullDescription.DocumentText = wbFullDescription.DocumentText.Replace("@@content@@", value);
                }
            }
        }

      public void CreateHtmlEditor(String strContent)
        {
            // String strFilePath = AppDomain.CurrentDomain.BaseDirectory + "tinymce\\examples\\index.html";
            String strBasePath = Path.GetDirectoryName(Application.ExecutablePath).Replace("\\bin\\Debug", string.Empty);
            String strFilePath = strBasePath + "\\tinymce\\examples\\index.html";
            if (File.Exists(strFilePath))
            {
                String strFileContent = File.ReadAllText(strFilePath);

                strFileContent = strFileContent.Replace("@@content@@", strContent);
                if (File.Exists(strBasePath + "\\tinymce\\examples\\HtmlEditor.html"))
                {
                    File.Delete(strBasePath + "\\tinymce\\examples\\HtmlEditor.html");
                }
                File.WriteAllText(strBasePath + "\\tinymce\\examples\\HtmlEditor.html", strFileContent);
            }
        }

Add two gridview column deatils to one gridview column


 <div style="background-color: White; height: 600px; overflow-y: scroll;">
                <asp:GridView ID="gvCategory" runat="server" AutoGenerateColumns="false"
                    Width="200px" Height="90%"
                    OnPageIndexChanging="gvSuppliers_PageIndexChanging1"
                    onrowdatabound="gvCategory_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="Category names">
                            <ItemTemplate>
                                <asp:LinkButton ID="lbtnCategory" runat="server"  CommandArgument='<%#Eval("CategoryID") %>'
                                    OnClick="lbtnCategory_Click"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerSettings Mode="NumericFirstLast" />
                </asp:GridView>
            </div>


  protected void lbtnCategory_Click(object sender, EventArgs e)
         {
             LinkButton lbtnSupplier = (LinkButton)sender;
             int categoryid = int.Parse(lbtnSupplier.CommandArgument);
             int intSupplierID = Convert.ToInt32(Session["intSupplierID"]);
             BindProductGrid(intSupplierID, categoryid);
          }

          protected void gvCategory_RowDataBound(object sender, GridViewRowEventArgs e)
          {
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
                LinkButton lbtn= (LinkButton) e.Row.FindControl("lbtnCategory");
                //Text='"<%#Eval("Name")"+"("+Eval("ProductCount")%>+")"'
                DataRow rowView = ((DataRowView)e.Row.DataItem).Row;
               
                if (lbtn != null)
                {
                    lbtn.Text = rowView["Name"] + " (" +  rowView["ProductCount"] + ")";
                }
              }
          }