Click here to Skip to main content
15,909,827 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My on button click code is as below. photo is not updating

C#
 protected void updatecompany_Click(object sender, EventArgs e)
{
            SqlConnection con = new SqlConnection("xyz...");
            con.Open();
string filepath = Server.MapPath("~/CompanyLogo/" + strFileName);
            string path = "~/CompanyLogo/" + strFileName;

            string str = "UPDATE Table1 set CompanyLogo='" + path +"'   where CompanyID=" + Convert.ToInt16(Request.QueryString["CompanyID"].ToString());

 SqlCommand cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
Posted
Comments
ZurdoDev 7-Mar-14 11:25am    
You are leaving yourself open to sql injection but otherwise it looks like the code should work. The easy thing to do is debug it and see what is happening.

The code you have shown of course does not include the uploading of the photo.

1 solution

First off, stop accessing SQL like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

The next thing to do is check your QueryString return value: if it is wrong, or empty, then the SQL will not find any matching records and will not update anything.

I'd also check the return value from ExecuteNonQuery - if it isn't one, then it didn't do what you wanted and you need to try to report or log the problem with as many details as you can.
 
Share this answer
 
Comments
jayraj86 7-Mar-14 11:35am    
I'm not getting you.. how to make changes i this code to update ?
OriginalGriff 7-Mar-14 11:40am    
You know how to do parameterized queries, I assume?
jayraj86 7-Mar-14 12:00pm    
No. I'm just a beginner
OriginalGriff 7-Mar-14 12:17pm    
You mean you are doing a website, and you are concatenating strings throughout?
You have been told about SQL Injection, haven't you?
If not, then google "Bobby Tables" and don't assume it's a joke...anyone could destroy your database from the other side of the world.
Forget your current problem, and fix that fast!
jayraj86 9-Mar-14 5:09am    
Thanks, man! I just changed the code to parameterized. now how I can use it to update a photo ?

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