Click here to Skip to main content
15,891,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am storing image name and image path in database.i want when image name is already exists it update the data but there is some problem.my this code it update the image but gives error "ExecuteNonQuery: Connection property has not been initialized."
actually i want if image name exists update it if not insert it???



C#
protected void Button2_Click1(object sender, EventArgs e)
        {
            {
                string username =(string)( Session["UserAuthentication"]);
                //Get Filename from fileupload control
               // string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
                //Save images into Images folder
                FileUpload1.SaveAs(Server.MapPath("Images/" + username));
                string str = "Images/" + username;
                //Getting dbconnection from web.config connectionstring
                SqlConnection con = new SqlConnection(connstring);
                //Open the database connection
                con.Open();
                //Query to insert images path and name into database

                //SqlCommand cmd = new SqlCommand("Insert into imagePath(imagename,imgpath) values(@imagename,@imgpath)", con);
                cmd=new SqlCommand("UPDATE imagepath SET imgpath='"+str+"' WHERE imagename='"+username+"'");
                //Passing parameters to query
               cmd.Parameters.AddWithValue(username, username);
               cmd.Parameters.AddWithValue(str, "Images/" + username);
                cmd.ExecuteNonQuery();
                //Close dbconnection
                con.Close();
                //Response.Redirect("~/Default.aspx");
            }
Posted
Comments
vakativamsi 14-Jun-13 2:02am    
Hi ,
define the connection in the query ending "CON"
replace the below line i8n you project

cmd=new SqlCommand("UPDATE imagepath SET imgpath='"+str+"' WHERE imagename='"+username+"'",con);


Thankyou

Add connection object in SqlCommand as second parameter. Try this:
C#
cmd=new SqlCommand("UPDATE imagepath SET imgpath='"+str+"' WHERE imagename='"+username+"'", con);


[Edit 1]
First you need to check the count in database for that image. If image exists in database the count will be more than zero(in this case 1). If count is coming more than zero then update the image otherwise insert that.
Try this:
C#
cmd=new SqlCommand("SELECT COUNT(*) FROM imagepath  WHERE imagename='"+username+"'", con);
int count  = Convert.ToInt32(cmd.ExecuteScalar());
if(count > 0){
    //Write update code here.
}
else{
    //Write insert code here.
}



--Amit
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 14-Jun-13 2:06am    
Oh man, you made it 6 mins before, 5'ed
_Amy 14-Jun-13 2:23am    
Thank you Prasad. :)
fak_farrukh 14-Jun-13 3:10am    
please now tell if imagename is already exists in database then update otherwise insert the data how will i do this???
_Amy 14-Jun-13 3:17am    
See my updated answer. :)
Hi Amit,

Nice answer.
One suggestion from my side.
Counting only the primary key (ID) instead of all columns (*) will be faster and is always preferred.

Thanks,
Tadit
C#
protected void Button2_Click1(object sender, EventArgs e)
        {
            {
                string username =(string)( Session["UserAuthentication"]);
                FileUpload1.SaveAs(Server.MapPath("Images/" + username));
                string str = "Images/" + username;
                SqlConnection con = new SqlConnection(connstring);
                con.Open();
                cmd=new SqlCommand("UPDATE imagepath SET imgpath='"+str+"' WHERE imagename='"+username+"'", con);//You need to mention connection string object here with command                
               cmd.Parameters.AddWithValue(username, username);
               cmd.Parameters.AddWithValue(str, "Images/" + username);
                cmd.ExecuteNonQuery();               
                con.Close();
            }
 
Share this answer
 
Comments
_Amy 14-Jun-13 2:22am    
+5! :)
Prasad_Kulkarni 14-Jun-13 2:29am    
Thank you Amit!
Hi ,
define the connection in the query ending   "CON"
replace the below line i8n you project

cmd=new SqlCommand("UPDATE imagepath SET imgpath='"+str+"' WHERE imagename='"+username+"'",con);


Thankyou
 
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