Tuesday 8 October 2013

No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.

How to Resolve No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type error in C#.NET?

You will get this type of problems when you pass Direct control ( TextBox ) as SQLParameter value.

EX:

 protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = WebConfigurationManager.AppSettings["ConnectionString"];
        SqlCommand cmd=new SqlCommand("insert into register values(@name,@address,@password,@confirmpassword)",con);  
        cmd.Parameters.AddWithValue("@name",TextBox1);
        cmd.Parameters.AddWithValue("@address", TextBox2);
        cmd.Parameters.AddWithValue("@password", TextBox3);
        cmd.Parameters.AddWithValue("@confirmpassword", TextBox4);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

If you observe the above code you are passing the TextBoxes as SQLParameter values.

You should pass the values of the Controls to SQLParameter as follows. 

 protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = WebConfigurationManager.AppSettings["ConnectionString"];
        SqlCommand cmd=new SqlCommand("insert into register values(@name,@address,@password,@confirmpassword)",con);  
        cmd.Parameters.AddWithValue("@name",TextBox1.Text);
        cmd.Parameters.AddWithValue("@address", TextBox2.Text);
        cmd.Parameters.AddWithValue("@password", TextBox3.Text);
        cmd.Parameters.AddWithValue("@confirmpassword", TextBox4.Text);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }




No comments: