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.
txb_name.Text. Also, you're executing your query twice withExecuteNonQuery()andExecuteScalar(). Drop the scalar execution.ExecuteNonQueryandExecuteScalar- if you run both, you're inserting the data twice. Just useExecuteNonQuery()only (which is the command to run anINSERT,DELETEorUPDATE- something that doesn't return a result set)