vk_devman
vk_devman

Reputation: 15

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

I have a receipttextbox field I am trying to have user's put in some value such as RE0001234 and query the database then return the value to a label. Right now as it stands if I put in my own value within the code in my query it returns the result to the label as expected but when asking for user input I get this error: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.

protected void receiptbox_TextChanged(object sender, EventArgs e)
{
    string getReceipt = receiptbox.Text;

}
protected void sealresultquery_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{

}
protected void searchbutton_Click(object sender, EventArgs e)
{

    sealresult.Text = "";
    receiptbox.Text = "";
    string getReceipt = receiptbox.Text;
    string connString = @"Data Source=SQL;Initial Catalog=XYZDATABASE;User ID=ADMIN;Password=*********";
    string query = "Select seal1 from dbo.RECEIPTHEADER where receipt = @getReceipt";
    SqlConnection conn = new SqlConnection(connString);
    SqlCommand comm = new SqlCommand(query, conn);
    comm.Parameters.Add(new SqlParameter(@getReceipt, receiptbox));


    using (conn)
    {
        try
        {
            conn.Open();
            SqlDataReader reader = comm.ExecuteReader();
            while(reader.Read())
            {
                sealresult.Text += reader[0].ToString();

            }
            reader.Close();
        }
        catch(Exception ex)
        {
            querystatus.Text = ex.Message;
        }

    }

}

Any suggestions as to what I am missing, any help is greatly appreciated!

Upvotes: 0

Views: 716

Answers (3)

Rob Johnston
Rob Johnston

Reputation: 879

When you called the Parameters.Add() method, you were using the Add(String,Object) overload, which has been marked as obsolete. I believe the value of the parameter could not be determined, so it was as if you were trying to find a receipt without an ID.

Take a look at the other methods of the SqlParameterCollection class to see what parameters they take. The second value of the Add() method is always the database type and never the value.

Try this:

comm.Parameters.Add(new SqlParameter(@getReceipt, SqlDbType.VarChar)).Value = receiptbox.Text;

or this (the same but shorter):

comm.Parameters.Add("@getReceipt", SqlDbType.VarChar).Value = receiptbox.Text;

You're also setting the value from the form to be empty with receiptbox.Text = "", so that should be removed. The line above and below this line don't add any value to the solution either.

Upvotes: 0

Serkan Arslan
Serkan Arslan

Reputation: 13393

.Text attribute missing end of the receiptbox control, change comm.Parameters.Add(... line with this

comm.Parameters.Add(new SqlParameter("@getReceipt", receiptbox.Text));

and move receiptbox.Text = ""; line to end of using, because it clears the input before using.

OR

change order of lines like this.

string getReceipt = receiptbox.Text;
receiptbox.Text = "";

and use getReceipt instead of receiptbox.Text

comm.Parameters.Add(new SqlParameter("@getReceipt", getReceipt));

Upvotes: 1

Nikolaus
Nikolaus

Reputation: 1869

You are setting things in wrong order:

receiptbox.Text = "";
string getReceipt = receiptbox.Text;

Try:

string getReceipt = receiptbox.Text;
receiptbox.Text = "";

Update: If you do it like that, then the Paramname is the Text of the TextBox:

comm.Parameters.Add(new SqlParameter(@getReceipt, receiptbox.Text));

Try this in addition to the other answers:

comm.Parameters.Add(new SqlParameter("@getReceipt", receiptbox.Text));

Upvotes: 0

Related Questions