Reputation: 69
super newbie on ASP.Net, and sorry if this has been asked countless times, i have read a lot of the answers to this similar problem here and on google too but i can't find a solution that applies to my code, i want to save the image url into the database, this is the code that isn't working:
if (imgPicture.ImageUrl != null)
{
String strConn = "Data Source=TOSHI;Initial Catalog=prueba;Integrated Security=True";
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
string strQuery = "Insert into curriculum (imagen) values (@imgPicture)";
cmd.CommandText = strQuery;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@imagen", imgPicture.ImageUrl);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
On the cmd.parameters i also tried
cmd.Parameters.Add("imagen", SqlDbType.VarChar).Value = imgPicture.ImageUrl;
But neither worked
Edit: The name of the field on the database is imagen, the image field on the ASP is imgPicture
Upvotes: 0
Views: 765
Reputation: 6238
You are using the wrong name for the parameter. It should be @imgPicture
, from the query:
cmd.Parameters.AddWithValue("@imgPicture", imgPicture.ImageUrl);
Update
Based on the comments to this question, I think the contents of this entire if
block can be removed and you can place the following into the main insert:
cmd.Parameters.AddWithValue("@imgPicture", (imgPicture.ImageUrl == null ? (object)DBNull.Value : (object)imgPicture.ImageUrl));
You will also need to add imagen to the end of the list of columns to insert in the query, and @imgPicture to the parameters.
Upvotes: 4
Reputation: 81645
Your query is this:
string strQuery = "Insert into curriculum (imagen) values (@imgPicture)";
where you are declaring @imgPicture
as your variable.
You need to use that variable in your code:
cmd.Parameters.Add("@imgPicture", SqlDbType.VarChar).Value = imgPicture.ImageUrl;
Upvotes: 2
Reputation: 116528
Unfortunately this has nothing to do with asp.net and everything to do with your query.
Your query is:
Insert into curriculum (imagen) values (@imgPicture)
which defines the parameter @imgPicture
.
Then you are adding a parameter called @imagen
(which will be ignored), but more importantly not adding the parameter @imgPicture
.
Are you sure you didn't mean cmd.Parameters.AddWithValue("@imgPicture", imgPicture.ImageUrl);
?
Upvotes: 2