Hello,
I just spendfew long hours trying to understand why I got this exception.
This is my code:
string
strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;"+
"Data Source=D:\\PlayerRegistered.mdb"; string strSQL = "INSERT INTO Players"+ "(FirstName,LastName,User,Code)"+ "VALUES('"+TextBox1.Text.ToString()+"','"+TextBox2.Text.ToString()+
"','"+TextBox3.Text.ToString()+
"','"+TextBox4.Text.ToString()+
"')"; OleDbConnection myConn = new OleDbConnection(strDSN); OleDbCommand myCmd = new OleDbCommand(strSQL,myConn); try{
myConn.Open();
myCmd.ExecuteNonQuery();
}
catch (Exception exp){
Console.WriteLine("Error:{0}", exp.Message);}
myConn.Close();
I checked my syntax dozens of times and I couldn't found nothing wrong in it.
can somone please help me???
MikaelPosted Sep 26, 2007, 8:12 AM
Hi, I am new in C# but have installed Visual Studio 2005 and created a simple MS Access db vith one table.
Running the following code works:
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;using
System.Data.OleDb;namespace
dbAccess{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e){
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Dokument\\Visual Studio 2005\\Projects\\dbAccess\\db.mdb"; string strSQL = "INSERT INTO aktiv (regnr) VALUES ('x')"; OleDbConnection myConnection = new OleDbConnection(strDSN); OleDbCommand myCmd = new OleDbCommand(strSQL,myConnection); try{
myConnection.Open();
myCmd.ExecuteNonQuery();
}
catch (Exception){
MessageBox.Show("Error.");}
finally{
myConnection.Close();
}
}
}
}
When trying to put values in the database in two fields doesnt work. Get the same Syntax error.
This is the code:
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;using
System.Data.OleDb;namespace
dbAccess{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e){
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Dokument\\Visual Studio 2005\\Projects\\dbAccess\\db.mdb"; string strSQL = "INSERT INTO aktiv (regnr,in) VALUES ('x','y')"; OleDbConnection myConnection = new OleDbConnection(strDSN); OleDbCommand myCmd = new OleDbCommand(strSQL,myConnection); try{
myConnection.Open();
myCmd.ExecuteNonQuery();
}
catch (Exception){
MessageBox.Show("Error.");}
finally{
myConnection.Close();
}
}
}
}
Lior KooznitsPosted Sep 17, 2007, 5:25 AM
Lior KooznitsPosted Sep 17, 2007, 5:20 AM
Bhasker DasPosted Sep 17, 2007, 4:43 AM
Jan MontanoPosted Sep 17, 2007, 1:52 AM
Remember to always put a space after each item you're concatenating if needed.
string strSQL = "INSERT INTO Players" should be string strSQL = "INSERT INTO Players " instead.
You forgot to put space after Players.
And also "(FirstName,LastName,User,Code)" should be "(FirstName,LastName,User,Code) " instead.
Forgot to put the space after Code)
=)