By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,459 Members | 1,472 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,459 IT Pros & Developers. It's quick & easy.

OleDb MSAccess Insert ???

P: n/a
What`s wrong with this ??? The insert does not add into the database.

Using a Winform and
Database is sc.mdb
Filename is supplier
Fields SuppNbr Int64
SuppName String
SuppPhone Int64

Here is the insert Class:

public void insert_data()
{
string source = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\sc.mdb";

string command1 = string.Format("select count(*) from supplier where
SuppNbr = {0}", Int64.Parse(textBox1.Text));

string command2 = string.Format("insert into supplier (
SuppNbr,SuppName,SuppPhone) values({0},'{1}',{2})", Int64.Parse
(textBox1.Text), textBox2.Text, Int64.Parse(textBox3.Text));

OleDbCommand iCommand = new OleDbCommand();
OleDbConnection iConnection = new OleDbConnection(source);

{
iConnection.Open();

iCommand.Connection = iConnection;
iCommand.CommandText=command1;
OleDbDataReader iReader1;

iReader1 = iCommand.ExecuteReader();
iReader1.Read();

{

if(iReader1.GetInt32(0) != 0)
{
iReader1.Close();
MessageBox.Show("Already exists");
iReader1.Close();
iConnection.Close();
}
else
{
iReader1.Close();
iCommand.CommandText=command2;
MessageBox.Show("Insert Done");
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
iConnection.Close();
}

}
}

}
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
from your code, I can't see where you use command 2, which will insert data
to the database. also your code is not completed, right?

"Michel R." <mi*********@videotron.ca> wrote in message
news:8f**************************@posting.google.c om...
What`s wrong with this ??? The insert does not add into the database.

Using a Winform and
Database is sc.mdb
Filename is supplier
Fields SuppNbr Int64
SuppName String
SuppPhone Int64

Here is the insert Class:

public void insert_data()
{
string source = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\sc.mdb";
string command1 = string.Format("select count(*) from supplier where
SuppNbr = {0}", Int64.Parse(textBox1.Text));

string command2 = string.Format("insert into supplier (
SuppNbr,SuppName,SuppPhone) values({0},'{1}',{2})", Int64.Parse
(textBox1.Text), textBox2.Text, Int64.Parse(textBox3.Text));

OleDbCommand iCommand = new OleDbCommand();
OleDbConnection iConnection = new OleDbConnection(source);

{
iConnection.Open();

iCommand.Connection = iConnection;
iCommand.CommandText=command1;
OleDbDataReader iReader1;

iReader1 = iCommand.ExecuteReader();
iReader1.Read();

{

if(iReader1.GetInt32(0) != 0)
{
iReader1.Close();
MessageBox.Show("Already exists");
iReader1.Close();
iConnection.Close();
}
else
{
iReader1.Close();
iCommand.CommandText=command2;
MessageBox.Show("Insert Done");
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
iConnection.Close();
}

}
}

}

Nov 15 '05 #2

P: n/a
"pei_world" <pe*******@hotmail.com> wrote in message news:<O#**************@TK2MSFTNGP10.phx.gbl>...
from your code, I can't see where you use command 2, which will insert data
to the database. also your code is not completed, right?


I am a C# Beginner. I have forgot to add the iCommand.ExecuteNonQuery();
So Command2 was never executed. Now it`s working fine.

Thanks anyway pei_world.
Michel
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.