2015-11-06 46 views
1

我曾尝试使用其他人在此使用的方法来解决他们的问题,但他们都没有为我工作。我是新来的ASP.NET框架,不明白为什么我想发送到我的数据库的信息不起作用。此外,视觉工作室在我尝试提交新数据之前不会给出错误,这使我很难确定问题。在System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常,但未在用户代码中处理。无法找出问题

namespace ylena_exercise 
{ 
    public partial class KnockoutBind : System.Web.UI.Page 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=ylena_exercise;Integrated Security=True"); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      con.Open(); 
      GridView1.Visible = false; 
     } 

     protected void AddBtn_Click(object sender, EventArgs e) 
     { 
      SqlCommand cmd = new SqlCommand("insert into Customers ('"+numid.Text+"','"+txtcustomer.Text+"','"+txtcontact.Text+"','"+txtaddress.Text+"','"+txtcity.Text+"','"+numpostcode.Text+"','"+txtcountry.Text+"')",con); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
      GridView1.DataBind(); 
      Label1.Visible = true; 
      Label1.Text = "Data Stored Successfully!"; 
      numid.Text = ""; 
      txtcustomer.Text = ""; 
      txtcontact.Text = ""; 
      txtaddress.Text = ""; 
      txtcity.Text = ""; 
      numpostcode.Text = ""; 
      txtcountry.Text = "";     
     } 
    } 
} 

dbo.Customers data file

Message after clicking "Submit" button

是不是有什么毛病我的数据?或者,也许问题是ExecuteNonQuery?

+0

尝试sql这种方式:insert into Customers(column1_name,column2_name)values('column1_value',column2_value)' – mshsayem

+1

通常'SqlException'文本包含SQL服务器提供的错误文本。更准确地阅读它,可能你会意识到原因。或者将它张贴在你的问题中,以便有人来帮助你。 –

回答

0

嘿,所以我设法找出原因的ExecuteNonQuery是给我的问题。事实证明,我只是没有将表列名称与SQL命令中的变量进行匹配。

对不起所有的麻烦家伙!不过,我欣赏所有有用的建议和建议。

2

你在你的insert声明错过Values你的代码应该是这样的:

SqlCommand cmd = new SqlCommand("insert into Customers values('"+numid.Text+"',.... 

而且你应该总是使用parameterized queries避免SQL Injection

SqlCommand cmd = new SqlCommand("insert into Customers values(@numid,..."); 
cmd.Parameters.AddWithValue("@numid",numid.Text); 
+0

感谢您的建议。我会尝试这种方式。 – gabed123

+0

@ gabed123 ...欢迎您。虽然直接指定类型并使用Value属性比AddWithValue更好。检查此:http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ –

+0

@Soner不幸的是,即使在作出调整后,它仍然是破并给我的消息“在System.Data.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理”,并且ExecuteNonQuery Connection属性尚未初始化 – gabed123

0

包括在你的SQL和组列名这样的值:

var command = "insert into customers (id, name, contact, address, city, postcode, country) values (@id, @name, @contact, @address, @city, @postcode, @country)"; 
SqlCommand cmd = new SqlCommand(command); 
cmd.Parameters.AddWithValue("@id", numid.Text); 
cmd.Parameters.AddWithValue("@name", txtcustomer.Text); 
cmd.Parameters.AddWithValue("@contact", txtcontact.Text); 
cmd.Parameters.AddWithValue("@address", txtaddress.Text); 
cmd.Parameters.AddWithValue("@city", txtcity.Text); 
cmd.Parameters.AddWithValue("@postcode", numpostcode.Text); 
cmd.Parameters.AddWithValue("@country", txtcountry.Text); 
cmd.ExecuteNonQuery(); 

[礼貌:Soner格尼尔& user2946329]

+3

'string.Format'不会**防止SQL注入漏洞。应提供准备好的陈述。 –

+0

我试过这种方式,我仍然在cmd.ExecuteNonQuery();消息“InvalidOperationException被用户代码置之不理,对不起,如果我看起来很无奈 – gabed123

+0

我已经知道了我的错误,谢谢@mshsayem。 – gabed123

相关问题