0

I am making a simple Login Form and getting the following Exception

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

Here is the C# code

        SqlConnection con = new SqlConnection("Data source=.;Initial catalog=InvoiceSystem;user id =sa;password=rfm");
        SqlCommand cmd = new SqlCommand("spLogin", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter p1 = new SqlParameter("username", txtUsername);
        SqlParameter p2 = new SqlParameter("password", txtPassword);
        cmd.Parameters.Add(p1);
        cmd.Parameters.Add(p2);
        try
        {
            con.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            if (rd.HasRows)
            {
                rd.Read();
                Response.Redirect("Default.aspx");
            }
            else
            {
                Response.Write("<script>alert('Invalid Username/Password')</script>");
            }
        }
        catch{

        }
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
Nuke
  • 1,169
  • 5
  • 16
  • 33

3 Answers3

3

Your trying to send TextBox objects to the stored procedure:

SqlParameter p1 = new SqlParameter("username", txtUsername);
SqlParameter p2 = new SqlParameter("password", txtPassword);

It doesn't know what to do with those objects. Instead, just send the text contained in those text boxes:

SqlParameter p1 = new SqlParameter("username", txtUsername.Text);
SqlParameter p2 = new SqlParameter("password", txtPassword.Text);
David
  • 208,112
  • 36
  • 198
  • 279
1

You are passing the TextBox itself into SqlParameter instead of it's text value. Change it to:

   SqlParameter p1 = new SqlParameter("username", txtUsername.Text);
   SqlParameter p2 = new SqlParameter("password", txtPassword.Text);
Jens R.
  • 191
  • 5
1

You have to extract the text using the .Text property like so:

SqlParameter p1 = new SqlParameter("username", txtUsername.Text);
SqlParameter p2 = new SqlParameter("password", txtPassword.Text);
Brian Mains
  • 50,520
  • 35
  • 148
  • 257