user2911539 user2911539 - 1 year ago 71
ASP.NET (C#) Question

System.Data.SqlClient.SqlException' has occured

I'm trying to store images on my ASP.NET web forms website, in an 'Images' folder.

Here is the code behind my submit button:

protected void btnSubmit_Click(object sender, EventArgs e)
{
//Get Filename from fileupload control
string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
//Save images into Images folder
fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));
//Getting dbconnection from web.config connectionstring
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BallinoraDBConnectionString1"].ToString());
//Open the database connection
con.Open();
//Query to insert images path and name into database
SqlCommand cmd = new SqlCommand("Insert into Group_Images(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
//Passing parameters to query
cmd.Parameters.AddWithValue("@ImageName", filename);
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
cmd.ExecuteNonQuery();
//Close dbconnection
con.Close();
Response.Redirect("~/Admin.aspx");
}


SQL Server table structure

Here is the error message I'm receiving

Answer Source

You have to update the column ID in your database, set it as Identity Increment

Identity Increment

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download