0

I don't know where the problem is. I'm trying to insert data from the textbox to the database and I get an error as shown below.

This is my code

private void but_Add_Click(object sender, EventArgs e)
{
    String query = "INSERT INTO Tbl_Cashier (FName, MName, LName, Address, ContactNo, Email, Age, Gender, Password, role) VALUES (@FName, @MName, @LName, @Address, @ContactNo, @Email, @Age, @Gender, @Password, @role)";

    using (SqlConnection connection = new SqlConnection(connectionString1))
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        connection.Open();

        command.Parameters.AddWithValue("@FName", txb_Fname);
        command.Parameters.AddWithValue("@MName", txb_Mname);
        command.Parameters.AddWithValue("@LName", txb_Lname);
        command.Parameters.AddWithValue("@Address", txb_Address);
        command.Parameters.AddWithValue("@ContactNo", txb_ContactNo);
        command.Parameters.AddWithValue("@Email", txb_Email);
        command.Parameters.AddWithValue("@Age", txb_Age);
        command.Parameters.AddWithValue("@Gender", txb_Gander);
        command.Parameters.AddWithValue("@Password", txb_Password);
        command.Parameters.AddWithValue("@role", txb_Role);

        command.ExecuteNonQuery();
        command.ExecuteScalar();
        connection.Close();
    }
}

The error I get is:

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: No mapping exists from object type System.Windows.Forms.TextBox to a known managed provider native type.

8
  • 6
    Use txb_name.Text. Also, you're executing your query twice with ExecuteNonQuery() and ExecuteScalar(). Drop the scalar execution. Commented Oct 4, 2017 at 18:54
  • oh okay ^^ ill try Commented Oct 4, 2017 at 18:55
  • thank you so much ^^ i got it , i almost going to ask you about why it became twice . thanks again^^. Commented Oct 4, 2017 at 19:05
  • 1
    Kudos on using paramaterized SQL as well. :D Commented Oct 4, 2017 at 19:18
  • You don't need to have both ExecuteNonQuery and ExecuteScalar - if you run both, you're inserting the data twice. Just use ExecuteNonQuery() only (which is the command to run an INSERT, DELETE or UPDATE - something that doesn't return a result set) Commented Oct 4, 2017 at 19:21

1 Answer 1

5

You need to add .Text end of control.

command.Parameters.AddWithValue("@FName", txb_Fname.Text);
command.Parameters.AddWithValue("@MName", txb_Mname.Text);
command.Parameters.AddWithValue("@LName", txb_Lname.Text);
command.Parameters.AddWithValue("@Address", txb_Address.Text);
command.Parameters.AddWithValue("@ContactNo", txb_ContactNo.Text);
command.Parameters.AddWithValue("@Email", txb_Email.Text);
command.Parameters.AddWithValue("@Age", txb_Age.Text);
command.Parameters.AddWithValue("@Gender", txb_Gander.Text);
command.Parameters.AddWithValue("@Password", txb_Password.Text);
command.Parameters.AddWithValue("@role", txb_Role.Text);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.