Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
3.80/5 (2 votes)
See more:
I have one form which contains one file upload control. on page load i am loading all data from database into the respective textboxes of form.

I have one update button but on click my data is not updating. below is my code of update button
When i change image it gets updated but not other fields


C#
protected void ChangeProfile_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile != null)
            {
                /* Get a reference to PostedFile object */
                HttpPostedFile attFile = FileUpload1.PostedFile;
                /* Get size of the file */
                int attachFileLength = attFile.ContentLength;
                /* Make sure the size of the file is > 0  */
                if (attachFileLength > 0)
                {
                    /* Get the file name */
                    strFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    /* Save the file on the server */
                    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/CompanyLogo/" + strFileName));

                    string filepath = Server.MapPath("~/CompanyLogo/" + strFileName);
                    string path = "~/CompanyLogo/" + strFileName;


                    SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
                    con.Open();
                    string str = "UPDATE Companies set FirstName='" + firstname.Text + "', LastName='" + lastname.Text + "', EmailID='" + email.Text + "', CompanyName='" + companyname.Text + "',Designation='" + designation.Text + "', ContactNo='" + mobileno.Text + "', Country='" + countrylist.SelectedItem.Text + "',Address='" + address.Text + "', CompanyCategory='" + companysector.SelectedItem.Text + "',NoOfEmployees='" + DropDownList5.SelectedItem.Text + "', Website='" + website.Text + "',AboutCompany='" + aboutcompany.Text + "', UserName='" + cusername.Text + "', CompanyLogo='" + path + "'   where CompanyID=" + Convert.ToInt16(Session["CompanyID"].ToString());

                    SqlCommand cmd = new SqlCommand(str, con);

                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                else
                {
                    Updateprofile();
                    
                }

            }

           
        }
        protected void Updateprofile()
        {
            SqlConnection con1 = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
            con1.Open();
            string str1 = "UPDATE Companies set FirstName='" + firstname.Text + "', LastName='" + lastname.Text + "', EmailID='" + email.Text + "', CompanyName='" + companyname.Text + "',Designation='" + designation.Text + "', ContactNo='" + mobileno.Text + "', Country='" + countrylist.SelectedItem.Text + "',Address='" + address.Text + "', CompanyCategory='" + companysector.SelectedItem.Text + "',NoOfEmployees='" + DropDownList5.SelectedItem.Text + "', Website='" + website.Text + "',AboutCompany='" + aboutcompany.Text + "', UserName='" + cusername.Text + "' where CompanyID=" + Convert.ToInt16(Session["CompanyID"].ToString());

            SqlCommand cmd1 = new SqlCommand(str1, con1);

            cmd1.ExecuteNonQuery();
            Response.Write("kccngj");
            con1.Close();
        }
Posted
Updated 11-May-14 4:48am
v2
Comments
Herman<T>.Instance 11-May-14 10:35am    
And what is your question?
jayraj86 11-May-14 10:36am    
I have one form which contains one file upload control. on page load i am loading all data from database into the respective textboxes of form.

I have one update button but on click my data is not updating. below is my code of update button
When i change image it gets updated but not other fields
José Amílcar Casimiro 11-May-14 10:53am    
Must have attention to sql injection.
Can you show the code of the Page_Load event.

1 solution

It is difficult to tell what is the issue with above code without knowing related other code of your application and the database structure.
Do as below,
1. create breakpoint on below line of both methods
C#
SqlCommand cmd = new SqlCommand(str, con);

2. run your application and check whether it hit on the break points as expected. If the application hit on one of the breakpoint, check the value of sql statement and manually run the sql statement on SQL server. you can confirm the sql statement correct or wrong by doing so. if you found any issue, come back to VS and do the changes or ask question with full details of your findings. if you there is any exceptions you need to include all those details when you ask question.
go and read Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900