Add connection object in
SqlCommand
as second parameter. Try this:
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:
cmd=new SqlCommand("SELECT COUNT(*) FROM imagepath WHERE imagename='"+username+"'", con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if(count > 0){
}
else{
}
--Amit