i have error :(

hi,
can anybody help me Sad

when i want to save the record in access database and i cannot save it

this is error
http://www.geocities.com/qtruni/eee.JPG

and this the code for save button

+++++++++++++++++++++++++++++++++++++++++++++++++++
private void Savebutton_Click(object sender, System.EventArgs e)
{
if (sidTextBox.Text != "" && fnameTextBox.Text != "" && lnameTextBox.Text != "" && nationalityTextBox.Text != "" && phoneTextBox.Text != "")
{
OleDbConnection myCon = new OleDbConnection(strCon);
// OleDbConnection myCon = new OleDbConnection( strCon ) ;
//the string to get values from the textboxes and form an "INSERT INTO"
// statement.
string sqlStr = "INSERT INTO student(sid, fname, lname, nationality, phone, major,college)"
+ " VALUES (" + this.sidTextBox.Text + ","
+ this.fnameTextBox.Text + ","
+ this.lnameTextBox.Text + ","
+ this.nationalityTextBox.Text + ","
+ this.phoneTextBox.Text + ","
+ this.majortextBox1.Text + ","
+ this.collegeComboBox.SelectedItem + ");";

// create Objects of ADOConnection and ADOCommand
OleDbConnection myConn = new OleDbConnection(strCon);
OleDbCommand myCmd = new OleDbCommand(sqlStr, myConn);
try
{
myConn.Open();
myCmd.ExecuteNonQuery();
}
catch (Exception ed)
{
MessageBox.Show("Error in inserting! " + ed.ToString(), "Error");
}
finally
{
myConn.Close();
}
}
}
++++++++++++++++++++++++++++
where is the error




Answer this question

i have error :(

  • msdmo

    i totally agree with Mario, even Microsoft have provided Designers to create queries.



  • SatishJ

    string sqlStr = "INSERT INTO student (sid, fname, lname, nationality, phone, major,college) VALUES ('" + this.sidTextBox.Text + "', '"

    + this.fnameTextBox.Text + "', '"

    + this.lnameTextBox.Text + "', '"

    + this.nationalityTextBox.Text + "', '"

    + this.phoneTextBox.Text + "', '"

    + this.majortextBox1.Text + "', '"

    + this.collegeComboBox.SelectedItem + "')";

    1) you forgot a single quote at the begining ,

    2) there is a spaces you forgot "',space'"

    3) there is no ";" at the end of your statment

    4) additionaly that could cause trouble because you can't be assure the user entered single quote in his data entry that will cause a problem so you have to replace it by 2 single quotes b4 adding it to the sql statment like for examle

    this.textBox1.Text.Replace("'", "''")

    //note the first string is single quote,but the last string is 2 single quotes not double quote

    hope this helps



  • bl4ckprint

    thank u ali

    but when i changed to this

    string sqlStr = "INSERT INTO student" + "(sid, fname, lname, nationality, phone, major,college)"

    + " VALUES (" + this.sidTextBox.Text + "','"

    + this.fnameTextBox.Text + "','"

    + this.lnameTextBox.Text + "','"

    + this.nationalityTextBox.Text + "','"

    + this.phoneTextBox.Text + "','"

    + this.majortextBox1.Text + "','"

    + this.collegeComboBox.SelectedItem + "');";

    it is gave me another error

    http://www.geocities.com/qtruni/e2.JPG

    why



  • Slyke

    I would say that you are missing a single quote:

    ... + " VALUES ('" + ...

    Building queries a piece at a time is never a good idea. Use parameters instead: they remove all the problems with type formatting.

    Hope this helps

    --mc


  • carywinton

    if you are using fname, lname.... etc as Varchar, then you have to provide the quotes

    for e.g

    string sqlStr = "INSERT INTO student( fname, lname)
    VALUES (" '+
    + this.fnameTextBox.Text + "' ,'"
    + this.lnameTextBox.Text + "');";

    // hope this will help



  • Jerick06

    You may use something like this (I am reducing the number of parameters because I'm lazy, but you'll get the idea). This is the easiest approach, alternatively you could also deal better with types.

    string sqlStr = "INSERT INTO students (sid, fname, lname) VALUES (@sid, @fname, @lname);";
    OleDbCommand myCmd = new OleDbCommand (sqlStr, myCon);
    myCmd.Parameters.AddWithValue ("@sid", sidTextBox.Text);
    myCmd.Parameters.AddWIthValue ("@fname", fnameTextBox.Text);
    myCmd.Parameters.AddWithValue ("@lname", lnameTextBox.Text);

    At this point the command is ready to be used.

    Keep in mind that the AddWithValue() method is new with VS2005. You can use the Add () method in VS2003, exactly with the same syntax.

    Have fun

    --mc


  • MGM54

    but it didn't save a record in the access database just save when my project open ,why

    mario and ali how can i use parameters



  • i have error :(