0

Hello I have a Stored Proc for the Registration Page, but I need ADO.NET to take values from various textboxes. 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. "

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.
Girish
  • 306
  • 3
  • 17

3 Answers3

2

Try TextBox1.Text, TextBox is the Control. The Text property holds the String value.

cmd.Parameters.Add(new SqlParameter("@EmpID", TextBox1.Text));
Matt
  • 3,143
  • 1
  • 17
  • 14
  • Hi, Sorry those were typo errors. However, I'm getting errors further like this- Non-invocable member 'System.Data.SqlClient.SqlCommand.Parameters' cannot be used like a method. When I used code like these_ cmd.Parameters.Add(new SqlParameter("@EmpID", SqlDbType.Int)); cmd.Parameters("@EmpID").Value = TextBox1.Text; – Girish Jul 20 '12 at 17:13
  • 1
    Can you re-post the code without typos? As it is, you're passing a System.Web.UI.WebControls.TextBox as an argument, which is not correct because it can't be converted to a native type understood by the SQL provider. The exception makes perfect sense based on the code you have there. – Matt Jul 20 '12 at 17:15
  • 1
    The new error is because you are using parentheses, making C# think you want to call a method called 'Parameters' with a string argument. You want to use brackets [ ] – Matt Jul 20 '12 at 17:18
  • Oh Sorry even I couldn't recognize thank you for that. But this is little tricky for me.. Now its coming up with other error- "Failed to convert parameter value from a String to a Int32." :( – Girish Jul 20 '12 at 17:22
  • 2
    Int32.Parse(TextBox1.Text), TextBox.Text is a string, the column in your database must be int, so the Int32.Parse(String) method will take a string and create an Int32 for you – Matt Jul 20 '12 at 17:24
  • Telling the same after editing:( Failed to convert parameter value from a String to a Int32. – Girish Jul 20 '12 at 17:26
0

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.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • I'm still receiving same error. Insert Error:Failed to convert parameter value from a String to a Int32. I did use "Convert.ToInt32(TextBox1.Text);" But no change in the results. – Girish Jul 20 '12 at 17:36
  • How about the EmpTYpe ? What type it is ? – Shyju Jul 20 '12 at 17:37
  • This is where the confusion is- Emptype is a FK in dbo.emp. There's 'Type' which is a PK in dbo.EmpType. DropDown1 value goes to dbo.EmpType table and then the TypeID(auto-generated) is updated as EmpType in dbo.Emp – Girish Jul 20 '12 at 17:39
  • This is where I'd cleared doubt of this concept: http://stackoverflow.com/questions/11575590/passing-value-to-one-table-and-updating-in-parent-table – Girish Jul 20 '12 at 17:41
0

why dont u use this format?

         cmd.Parameters.Add("@parameter", SqlDBType , size).value = TextBox1.Text
dansasu11
  • 875
  • 1
  • 9
  • 17
  • But I do not think that is a problem here. This is where the confusion is- Emptype is a FK in dbo.emp. There's 'Type' which is a PK in dbo.EmpType. DropDown1 value goes to dbo.EmpType table and then the TypeID(auto-generated) is updated as EmpType in dbo.Emp – Girish Jul 20 '12 at 17:45