2014-03-12 42 views
0

我在SQL Server Management Express中有一个名为“Table_1”的表。我已经成功地将我的ASP程序连接到数据库。我已经编码了以下编码以便在数据库中插入数据。如何在使用ASP.NET的插入代码中处理异常?

ExecuteNonQuery: Connection property has not been initialized. 
    Description: An unhandled exception occurred during the execution of the current web  request. Please review the stack trace for more information about the error and where it originated in the code. 
    Exception Details: System.InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized. 
    Source Error: 
    Line 34:    { 
    Line 35:     SqlCommand hi = new SqlCommand("insert into Table_1 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "'),conn"); 
    Line 36:     hi.ExecuteNonQuery(); 
    Line 37:     conn.Close(); 
    Line 38:    } 

请帮我一个想法,以清除异常:

using System; 
    using System.Collections; 
    using System.Configuration; 
    using System.Data; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.HtmlControls; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Xml.Linq; 
    using System.Data.SqlClient; 
    namespace k2 
    { 
    public partial class _Default : System.Web.UI.Page 
    { 
    SqlConnection conn = new SqlConnection("Data Source=ARTHY/SQLEXPRESS;Initial     Catalog=k2;Integrated Security=True"); 
    protected void Page_Load(object sender, EventArgs e) 
    {   
    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
    try 
    { 
    conn.Open();   
    } 
    catch 
    { 
    SqlCommand hi = new SqlCommand("insert into Table_1 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "'),conn"); 
    hi.ExecuteNonQuery(); 
    conn.Close(); 
    }  
    } 
    } 
    } 

当我试图插入任何数据,我如下得到一个例外。

+0

首先,为什么这个代码在catch块中,而它应该在'conn.Open()'之后? –

+0

我刚刚尝试清除异常。在给try和catch块之前,例外是在“conn.Open();”。然后在实现try和catch之后,它转到“hi.ExecuteNonQuery();”。 – kalyan

回答

0

您的conn.Open()行位于try{}块中,而其余代码位于catch{}块中。所以,conn.Open()行会抛出异常,触发catch{},现在没有conn引用。四处移动你的代码:

try 
{ 
    conn.Open();   
    SqlCommand hi = new SqlCommand("insert into Table_1 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')", conn); 
    hi.ExecuteNonQuery(); 
    conn.Close(); 
} 
catch 
{ 
    //do something else if there is an exception 
} 
+0

现在,异常已被清除。但是,数据并未插入到数据库中。没有错误或异常,但也没有数据插入到数据库中。 – kalyan

+1

哎呀,刚注意到SqlCommand行末尾的“,conn”是用引号引起来的 - 它本该不是。 –

+0

是的,我试过了,但只有相同的结果。我改变了这样的:SqlCommand hi = new SqlCommand(“Insert into Table_1 values('”+ TextBox1.Text +“','”+ TextBox2.Text +“' ,'“+ TextBox3.Text +”','“+ TextBox4.Text +”','“+ TextBox5.Text +”','“+ TextBox6.Text +”')“,conn); – kalyan

0

除了马特的答案,我会建议包装连接到using语句,以确保连接总是正确关闭。这里是我的实现(重构一点点)

try 
{ 
    using (var connection = new SqlConnection("Data Source=ARTHY/SQLEXPRESS;Initial Catalog=k2;Integrated Security=True")) 
    { 
     connection.Open(); 
     var commandText = string.Format("insert into Table_1 values('{0}','{1}','{2}','{3}','{4}','{5}')", 
             TextBox1.Text, 
             TextBox2.Text, 
             TextBox3.Text, 
             TextBox4.Text, 
             TextBox5.Text, 
             TextBox6.Text); 

     var command = new SqlCommand(commandText, connection); 
     command.ExecuteNonQuery(); 
    } 
} 
catch (Exception exception) 
{ 
    // do something with exception 
} 
+0

顺便说一句,考虑使用SqlParameters来避免SQL注入 –

+0

只是注意到,我忘记在执行命令之前打开连接。固定。 –