简体   繁体   English

存储过程ADO.NET错误

[英]Stored Procedure ADO.NET error

Hello I have a Stored Proc for the Registration Page, but I need ADO.NET to take values from various textboxes. 你好,我有注册页面的存储过程,但我需要ADO.NET从各种文本框中获取值。 However, I'm recieving error like this: 但是,我收到这样的错误:

"System.ArgumentException: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type. " “System.ArgumentException:从对象类型System.Web.UI.WebControls.TextBox到已知的托管提供程序本机类型不存在映射。”

public void InsertInfo()
    {
        String empdb = @"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True";
        SqlConnection conn = new SqlConnection(empdb);
        SqlCommand cmd = new SqlCommand("bridge_Type", conn);
        cmd.CommandType = CommandType.StoredProcedure;


        try
        {
            conn.Open();
            cmd.Parameters.Add(new SqlParameter("@EmpID", TextBox1.Text));
            cmd.Parameters.Add(new SqlParameter("@Name", TextBox2.Text));
            cmd.Parameters.Add(new SqlParameter("@Mob2", TextBox3.Text));
            cmd.Parameters.Add(new SqlParameter("@Email", TextBox14.Text));
            cmd.Parameters.Add(new SqlParameter("@Emptype", dropdown1.SelectedValue));
            cmd.ExecuteNonQuery();
        }

        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);

        }

        finally
        {
            if (conn != null)
            {
                conn.Close();
            }
        }

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        InsertInfo();
    }

Then I used this format to add values from controls: 然后我使用这种格式来添加控件的值:

cmd.Parameters.Add(new SqlParameter("@EmpID", SqlDbType.Int));
cmd.Parameters("@EmpID").Value = TextBox1.Text;

I'm getting errors on: 我收到错误:

Its showing errors for these kind of codes by appearing red line under 'Parameters'.

Non-invocable member 'System.Data.SqlClient.SqlCommand.Parameters' cannot be used like a method.

Try TextBox1.Text, TextBox is the Control. 尝试TextBox1.Text,TextBox是Control。 The Text property holds the String value. Text属性保存String值。

cmd.Parameters.Add(new SqlParameter("@EmpID", TextBox1.Text));

Try this code. 试试这个代码。

try
{
  string empdb = @"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True";
  using (var conn = new SqlConnection(connString))
  {
    using (var cmd = new SqlCommand("bridge_Type",conn))
    {           
        cmd.CommandType = CommandType.StoredProcedure;           
        cmd.Parameters.AddWithValue("@EmpID",TextBox1.Text);
        cmd.Parameters.AddWithValue("@Name", TextBox2.Text);    
        cmd.Parameters.AddWithValue("@Mob2", TextBox3.Text); 
        cmd.Parameters.AddWithValue("@Email", TextBox14.Text); 
        cmd.Parameters.AddWithValue("@EmpType",dropdown1.SelectedValue); 
        conn.Open();
        cmd.ExecuteNonQuery();           
    }
  }
}
catch(Exception ex)
{
   string msg = "Insert Error:"+ ex.Message;
   throw new Exception(msg);
}

Make sure you are converting to the types same as the Parameter types in your stored proc ( Ex: If the parameter type of EmpID is int, you may need to convert the TextBox1.Text value to int. Check for null values also. 确保转换为与存储过程中的参数类型相同的类型(例如:如果EmpID的参数类型为int,则可能需要将TextBox1.Text值转换为int。还要检查空值。

为什么不使用这种格式?

         cmd.Parameters.Add("@parameter", SqlDBType , size).value = TextBox1.Text

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

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