Results 1 to 5 of 5

Thread: [02/03] Storing data to table

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    33

    [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?

  2. #2
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    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.
    "The Force will be with you, always."

    --Ben Kenobi--

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    33

    Re: [02/03] Storing data to table

    thankx... the problem is solved.!!!

  4. #4
    Addicted Member
    Join Date
    Jul 2002
    Location
    Cleveland, Ohio
    Posts
    185

    Re: [02/03] Storing data to table

    glad to help
    "The Force will be with you, always."

    --Ben Kenobi--

  5. #5
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width