简体   繁体   English

使用C#/ Asp.Net / SQL进行INSERT错误

[英]INSERT error using C#/Asp.Net/SQL

I have tried to run the query to insert data into the database, but I got error while runnnig the code.. 我试图运行查询将数据插入数据库,但我在运行代码时遇到错误..

ExecuteNonQuery requires an open and available connection. ExecuteNonQuery需要一个开放且可用的连接。 The connection's current state is closed. 连接的当前状态已关闭。

Could you please tell me what the error is and why it happened? 你能告诉我错误是什么以及它为什么会发生吗?

{
    con2.Open();

    if (TextBox1.Text == "")
    {
        Response.Write("<script>alert('please enter Login Name')</script>");
    }
    else if (TextBox2.Text == "")
    {
        Response.Write("<script>alert('please enter Password')</script>");
    }
    else if (TextBox3.Text == "")
    {
        Response.Write("<script>alert('please enter Confirm Password')</script>");
    }
    else
    {
        //if (TextBox2.Text == TextBox3.Text)
        //{

            string a;
            a = "insert into tbl_Purchase_Users(Login_Name, Password, Uname, Uid, EmailID, Role, Status) values(@LName, @Pswd, @Uname, @uid, @Eid, @role, @stat)";
            SqlCommand cm = new SqlCommand(a, con1);
            cm.Parameters.AddWithValue("@LName", TextBox1.Text);

            string original;
            original = TextBox2.Text.Trim();
            int h = original.GetHashCode();
            string withHash = original;
            b1 = Encoding.BigEndianUnicode.GetBytes(withHash);
            encrypted = Convert.ToBase64String(b1);
            cm.Parameters.AddWithValue("@Pswd", encrypted);
            cm.Parameters.AddWithValue("@Uname", TextBox3.Text);
            cm.Parameters.AddWithValue("@uid", TextBox4.Text);
            cm.Parameters.AddWithValue("@Eid", TextBox5.Text);
            cm.Parameters.AddWithValue("@role", TextBox6.Text);
            cm.Parameters.AddWithValue("@stat", TextBox7.Text);

            cm.ExecuteNonQuery();
            Response.Write("<Script>alert('inserted')</script>");
        }
        con2.Close();
    }

You have opened con2 only not con1. 你已经打开了con2而不是con1。 You passed con1 in SqlCommand. 你在SqlCommand中传递了con1。 Use the below code: 使用以下代码:

SqlCommand cm = new SqlCommand(a, con2);

You are opening the connection called con2 but you are using con1 on you SqlCommand. 您正在打开名为con2的连接,但您在SqlCommand上使用con1。

From what I can see you haven't opened con1 从我可以看到你没有打开con1

It looks like you have two different SqlConnection objects - con1 and con2. 看起来你有两个不同的SqlConnection对象 - con1和con2。 You are opening con2, but passing in con1 to the SqlCommand constructor. 您正在打开con2,但是将con1传递给SqlCommand构造函数。

As the error message states, the connection that you are using must be open. 如错误消息所述,您正在使用的连接必须是打开的。

If you pass con2 to the SqlCommand constructor, or if you open con1, your code should work. 如果将con2传递给SqlCommand构造函数,或者打开con1,则代码应该可以正常工作。

确保con1con1 ,因为您的调用中显示的唯一.Open()调用与con2相关

con1.Open();

just use one connection object like 只需使用一个连接对象

 con2.Open();
 SqlCommand cm = new SqlCommand(a, con2)

I can't see con1.open(). 我看不到con1.open()。 you have used Con1 in Sqlcommand. 你在Sqlcommand中使用过Con1。 Please open Con1.Open(); 请打开Con1.Open();

First you need to connect with database by using Open(), in ur case con1.Open(); 首先,你需要使用Open()连接数据库,在你的情况下con1.Open(); then perform action and close the connection. 然后执行操作并关闭连接。 con1.Close(); con1.Close(); else { //if (TextBox2.Text == TextBox3.Text) //{ else {// if(TextBox2.Text == TextBox3.Text)// {

        string a;
        a = "insert into tbl_Purchase_Users(Login_Name,Password,Uname,Uid,EmailID,Role,Status) values(@LName,@Pswd,@Uname,@uid,@Eid,@role,@stat)";
        SqlCommand cm = new SqlCommand(a, con1);
        con1.Open();
        cm.Parameters.AddWithValue("@LName", TextBox1.Text);

        string original;
        original = TextBox2.Text.Trim();
        int h = original.GetHashCode();
        string withHash = original;
        b1 = Encoding.BigEndianUnicode.GetBytes(withHash);
        encrypted = Convert.ToBase64String(b1);
        cm.Parameters.AddWithValue("@Pswd", encrypted);
        cm.Parameters.AddWithValue("@Uname", TextBox3.Text);
        cm.Parameters.AddWithValue("@uid", TextBox4.Text);
        cm.Parameters.AddWithValue("@Eid", TextBox5.Text);
        cm.Parameters.AddWithValue("@role", TextBox6.Text);
        cm.Parameters.AddWithValue("@stat", TextBox7.Text);

        cm.ExecuteNonQuery();
        Response.Write("<Script>alert('inserted')</script>");
        con1.Close();

} }

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

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