ASP.NET:错误 - 列名称或提供的值数与表定义不匹配

时间:2015-07-29 14:24:17

标签: c# sql asp.net upload

我正在使用ASP.NET Web应用程序项目。在我的注册页面中,我要求用户上传照片,但当我尝试上传时,此错误显示在第83行的浏览器中:

Line 81:                 comm.Parameters.AddWithValue("@License", License);
Line 82:                 conn.Open();
Line 83:                 int row = comm.ExecuteNonQuery();
Line 84:                 conn.Close();
Line 85:                 if (row > 0) { LabelError.Text = "DONE"; }

我的数据库表:

CREATE TABLE [dbo].[DeliveryMen] (
    [Delivery_ID] INT             IDENTITY (1, 1) NOT NULL,
    [Name]        NVARCHAR (50)   NOT NULL,
    [Username]    NVARCHAR (50)   NOT NULL,
    [Password]    NVARCHAR (50)   NOT NULL,
    [Email]       NVARCHAR (50)   NOT NULL,
    [Phone]       NVARCHAR (50)   NOT NULL,
    [City]        NVARCHAR (50)   NOT NULL,
    [License]     VARBINARY(MAX) NOT NULL,
    [Latitude]    NUMERIC (18)    NOT NULL,
    [Longitude]   NUMERIC (18)    NOT NULL,
    PRIMARY KEY CLUSTERED ([Delivery_ID] ASC)
);

如果您需要插入功能:

 public void insert()
        {

            if (FileUpload1.PostedFile.FileName != "")
            {
                byte[] License;
                Stream s = FileUpload1.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(s);
                License = br.ReadBytes((Int32)s.Length);
                 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "insert into DeliveryMen values(@License) SELECT '7'";
                comm.Parameters.AddWithValue("@License", License);
                conn.Open();
                int row = comm.ExecuteNonQuery();
                conn.Close();
                if (row > 0) { LabelError.Text = "DONE"; }
            }
            else LabelError.Text = "Please upload the photo";
        }

1 个答案:

答案 0 :(得分:2)

正如SonerGönül在评论中提到的那样,将SQL更改为列出column name(s)parameter value(s)

comm.CommandText = "insert into DeliveryMen (License) values (@License);";