简体   繁体   English

为什么我得到ExecuteNonQuery(); 做主键后出错?

[英]Why I get ExecuteNonQuery(); error after doing primary key?

After i put CustID int and auto incremental in database it get error.. but after i remove it.. it will be okay to saved in database... the problem now i want to add CustID in int or primarykey that work.. and how do i do pop out/display message like the item was saved? 在我将CustID int并在数据库中自动递增后,它得到错误..但是在我将其删除后..将其保存在数据库中是可以的...问题现在我想在工作的int或primarykey中添加CustID。我如何弹出/显示类似该项目已保存的消息?

this is my SQL database.. 这是我的SQL数据库。

Custid int (could not work) help
custname varchar50
custadd varchar50
custphone varchar
custemail varchar50

these is my code : 这些是我的代码:

public partial class NewCase : System.Web.UI.Page
{
    SqlConnection con =new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\WebSite6\\App_Data\\ASPNETDB.MDF;Integrated Security=True;User Instance=True");
    SqlCommand com = new SqlCommand("Select * from aspnet_Customer");

public void Bind()
{
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataSet ds = new DataSet();
    con.Open();
    com.Connection = con;
    com.ExecuteNonQuery();
    da.Fill(ds, "aspnet_Customer");
    con.Close();
}


protected void Button1_Click1(object sender, System.EventArgs e)
{
    con.Open();
    SqlCommand comi = new SqlCommand("Insert into aspnet_Customer values (@Custname,@Custaddress,@Custemail,@Custphone)");
    comi.Parameters.Add("Custname", SqlDbType.VarChar, 50);
    comi.Parameters["Custname"].Value = TextBox3.Text;
    comi.Parameters.Add("Custaddress", SqlDbType.VarChar, 50);
    comi.Parameters["Custaddress"].Value = TextBox9.Text;
    comi.Parameters.Add("Custemail", SqlDbType.VarChar, 50);
    comi.Parameters["Custemail"].Value = TextBox10.Text;
    comi.Parameters.Add("Custphone", SqlDbType.VarChar, 50);
    comi.Parameters["Custphone"].Value = TextBox11.Text;


    comi.Connection = con;
    comi.ExecuteNonQuery();
    con.Close();
    Message.box("DATA ADDED SUCCESSFULLY!!");
    Bind();

}

} }

You must list column names in INSERT INTO clause in case if you have autoincrement column, so you can't insert all columns (your autoincrement column generates automatically). 如果必须具有自动增量列,则必须在INSERT INTO子句中列出列名,以免插入所有列(自动增量列会自动生成)。

SqlCommand comi = new SqlCommand(
            "Insert into aspnet_Customer( Custname, Custaddress, Custemail, Custphone)
                  values (@Custname,@Custaddress,@Custemail,@Custphone)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM