发生了System.Data.SqlClient.SqlException'

时间:2016-11-15 16:49:37

标签: c# asp.net sql-server webforms

我正在尝试将图像存储在我的ASP.NET Web表单网站的“Images”文件夹中。

以下是我的提交按钮背后的代码:

 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

1 个答案:

答案 0 :(得分:1)

您必须更新数据库中的列ID,将其设置为Identity Increment

Identity Increment