ADO add to database problem, help?

  • Thread starter Thread starter jbardrof
  • Start date Start date
J

jbardrof

Okay so i'm having some trouble with adding to my database through the
dataset. i can read from it and it prints just fine, i run the code
okay with no errors, but it simply doesn't add the record, i've left
it simple here just so i can get it right before i start adding the
extra functionality. okay so the table name is UserID, and i want to
add into the table, what am i doing wrong?

private void btnAddRecord_Click(object sender, System.EventArgs
e)
{
OleDbConnection AddConnection = new
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\salesleadmgmt.mdb");
OleDbDataAdapter addAdapter = new OleDbDataAdapter("Select *
from UserID", AddConnection);
DataSet addSet = new DataSet();
DataRow addRow;
OleDbCommandBuilder addCommandBuilder = new
OleDbCommandBuilder(addAdapter);

addAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
addAdapter.Fill(addSet, "UserID");
Console.WriteLine("i was able to load the userid
table.");

addRow = addSet.Tables["UserID"].NewRow();
addRow["UsrName"] = "Bob";
addRow["passwr"] = "Bobpwd";
addRow["UsrID"] = "6";
addSet.Tables["UserID"].Rows.Add(addRow);
Console.WriteLine("i inserted the new values");

}

this runs in the .CS file, i don't get any of the write lines or
anything, it does run on a button click, i have another button click
that works fine, i'll show that one, whats wrong with this first
one?

working display click
[code:1:e515321317]
private void Button1_Click(object sender, System.EventArgs e)
{
string ConnectString =@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\salesleadmgmt.mdb";
OleDbConnection CustConnection = new
OleDbConnection(ConnectString);
CustConnection.Open();

DataSet CustSet = new DataSet();
OleDbDataAdapter CustAdapt = new OleDbDataAdapter("SELECT *
FROM CustInfo", CustConnection);
CustAdapt.Fill(CustSet);
CustConnection.Close();
CustGrid.DataSource = CustSet;
CustGrid.DataBind();
}
[/code:1:e515321317]

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
Hi,

You are adding row only to the dataset.
You should store the new data to database using adapter.Update method.
 
heres what i got now, and its still not working, my database is
salesleadmgmt.mdb the table is UserID with columns UsrName and passwr
and UsrID, also ID is the primary key. what do i need to do to get
this to work?


private void Button1_Click(object sender, System.EventArgs e)
{
try
{
string ConnectString =@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\salesleadmgmt.mdb";
OleDbConnection AddConnection = new
OleDbConnection(ConnectString);
OleDbDataAdapter addAdapter = new
OleDbDataAdapter("Select * from UserID",
AddConnection);
addAdapter.ContinueUpdateOnError = false;
DataSet addSet = new DataSet();
DataRow addRow;
OleDbCommandBuilder addCommandBuilder = new
OleDbCommandBuilder(addAdapter);
addAdapter.MissingSchemaAction =
MissingSchemaAction.AddWithKey;
addAdapter.Fill(addSet, "UserID");
addRow =
addSet.Tables["UserID"].NewRow();
addRow["UsrName"] = "Bob";
addRow["passwr"] = "Bobpwd";
//addRow["UsrID"] = "6";
addSet.Tables["UserID"].Rows.Add(addRow);
OleDbCommand cmd = new OleDbCommand("INSERT INTO UserID
(UsrName, passwr) VALUES (@username,
@Password)", AddConnection);
cmd.Parameters.Add("@username", OleDbType.Char, 50,
"UsrName");
cmd.Parameters.Add("@Password", OleDbType.Char, 50,
"passwr");
addAdapter.InsertCommand = cmd;
addAdapter.Update(addSet, "UserID");
addSet.AcceptChanges();
addAdapter.Fill(addSet, "UserID");
UsrGrid.DataSource = addSet;
UsrGrid.DataBind();
//Console.WriteLine("i inserted the new
values");
}
catch( Exception ex)
{
string s = ex.Message;
System.Console.WriteLine( " ERROR:" +
ex.Message );
}
}

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
Back
Top