[02/03] Storing data to table
i'm having a problem in storing the data from the form that i created... Error Message given is "There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement."
My code is as below:
Code:
strSQL = "INSERT INTO supplier(Sup_id, Sup_Pwd, Sup_Name, Sup_Type, Sup_Tel, Sup_Add, Sup_Email, LastModifiedBy) " & _
"VALUES ('" & txtID.Text & "','" & txtPwd.Text & "','" & txtName.Text & "','" & ddlType.SelectedItem.Text & "'," & _
"'" & txtTel.Text & "','" & txtAdd.Text & "','" & txtEmail.Text & "','Yen Yen')"
is it anyproblem wif my code?
Re: [02/03] Storing data to table
Usually this error means that you have one too many arguments in the "VALUES" section of your statement. I don't see anything extra there, so I would double check all your commas and make sure you have them everywhere they are supposed to be and don't have two values running together.
Jim P.
Re: [02/03] Storing data to table
thankx... the problem is solved.!!!
Re: [02/03] Storing data to table
Re: [02/03] Storing data to table
It should be noted that this is not the most correct way to create a sql statement. It is considered best practice to use the parameters.AddWithValue
have a look at the following code, although it is c sharp it can be easily translated to VB
Code:
SqlConnection sqlConn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlConn;
//If you don't have a stored proc you would ignore this next line of code
//as SqlCommand defaults to text anyway.
cmd.CommandType = CommandType.StoredProcedure;
//So if you didn't have a stored proc you would simply put in the SQL
//in CommandText i.e.
//cmd.CommandText = "INSERT INTO Topic (Header, Title, htmlUrl) VALUES (@Header, @Title, @htmUrl);
cmd.CommandText = "InsertNewTopic";
cmd.Parameters.AddWithValue("@Header", System.Data.OleDb.OleDbType.VarChar).Value = txtHeader.Text;
cmd.Parameters.AddWithValue("@title", System.Data.OleDb.OleDbType.VarChar).Value = txtTitle.Text;
cmd.Parameters.AddWithValue("@htmlUrl", System.Data.OleDb.OleDbType.VarChar).Value = txtHtmlUrl.Text;
try
{
sqlConn.Open();
int i = cmd.ExecuteNonQuery();
sqlConn.Close();
lblSqlResult.Text = "Result was: ";
lblSqlResult.Text += i.ToString();
}
catch (Exception ex)
{
lblSqlResult.Text = ex.ToString();
}
pnlAddNew.Visible = false;
Hope this helps... I would also strongly suggest looking into the use of stored procedures as they really speed things up.
Thanks