index > Visual C# General > i have error :(

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




alemadi
alemadi
if you are using fname, lname.... etc as Varchar
ALI RAZA SHAIKH

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




alemadi
alemadi

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

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

Ali Raza Shaikh

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




alemadi
alemadi

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

Mario Cossi

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




http://fromisraeltolebanon.info
shakalama
reply 8

You can use google to search for other answers

 

More Articles

• How do I enable Ctrl + V and Ctrl + C for a ComboBox?
• C# 3.0 in Windows Vista
• ArrayLists + ref keyeord + accessing values automatically
• Build hi performance gridview in VS2005 with C#
• How to zip files using C#
• MessageBox
• Component licensing How do I?
• language arabic
• Why doesn't this work?!
• DirectX app crashing
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Creating WallPapers using c#
• need return values from Threads
• how to use the OleDbCommandBuilder in C
• How can I code this?
• flash file
• XML parsing
• casting problem from SD.DbType to SD.Sql
• How can I hide the caret of a RichTextBo
• OLEDB Row update error
• Can I change The direction Of the form??
• Sending email using enduser's default ma
• problem with Deserializing a string.
• How to go to a specific line of text fil
• How can I make a treeview node selected?
• Passing Values from Form A to Form B and

Hot Articles

• get a list of all local drives?
• referencing undefined xml attributes
• upcasting
• problem with Deserializing a string.
• string and object class
• Is there metadata/manifest for the Proje
• Determining Class Dependencies
• Convert VC 6.0 to C#
• Opening file in when clicking treeview n
• Newbie question...odd or even?
• CreateGraphic().Clip?
• A real easy one
• Printing to a dot matrix printer without
• How to get the ASP.Net controls
• Accessing Identical Webservices On Vario

Recommend Articles

• made setup to application with tow langu
• Custom collection like DataSet architect
• using Arrays with unknown size
• Errors while viewing usercontrol's desig
• Export PowerPoint with C#?
• time
• how can i delay compiler??
• Not executing some event
• virtual method or virtual class?
• How use Paging DataGird in WinForm
• How can I select a cell in DataGridView ?
• How to send c++ unsigned char in C#
• writing text from C# into XML (urgent)
• access database which located in differe
• I Lost My Object's Reference In The Curr