N
Norton
I am pulling my hair out over this one. I have done this same kind of thing in VB.Net but in C# it doesn't work. I dont' receive any errors either, it just won't update the pubs.Authors table. Here is the code:
private void CodedDataGrid_Load(object sender, System.EventArgs e)
{
//create a new connection object
String ConnectString = "Server=" + "MyMachine" + "; " + "User ID=" + "sa" + "; Password=" + "password" + "; initial catalog = " + "pubs" + ";";
conn = new SqlConnection(ConnectString); //create a new adapter
adp = new SqlDataAdapter("Select au_id,au_lname,au_fname,contract FROM authors",conn);
//creat ea new dataset
ds = new DataSet();
adp.Fill(ds);
this.dbGrid.DataSource = ds;
}
private void btnNew_Click(object sender, System.EventArgs e)
{
AdapterInsert();
//add a new record to the dataset, but first
try
{
adp.Update(ds,"Table");
}
catch (System.Data.SqlClient.SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AdapterInsert()
{
insComm = new SqlCommand();
adp.InsertCommand = insComm;
adp.InsertCommand.Connection = conn;
adp.InsertCommand.CommandType =
CommandType.StoredProcedure;
adp.InsertCommand.CommandText = "InsertAuthor";
SqlParameter MySqlParam1 = new SqlParameter("@au_id",SqlDbType.VarChar);
MySqlParam1.Value = this.txtID.Text;
MySqlParam1.SourceColumn = "au_id";
adp.InsertCommand.Parameters.Add(MySqlParam1);
SqlParameter MySqlParam2 = new SqlParameter("@au_fname",SqlDbType.VarChar);
MySqlParam2.Value = this.txtFName.Text;
MySqlParam2.SourceColumn = "au_fname";
adp.InsertCommand.Parameters.Add(MySqlParam2);
SqlParameter MySqlParam3 = new SqlParameter("@au_lname",SqlDbType.VarChar);
MySqlParam3.Value = this.txtLName.Text;
MySqlParam3.SourceColumn = "au_lname";
adp.InsertCommand.Parameters.Add(MySqlParam3);
SqlParameter MySqlParam4 = new SqlParameter("@contract",SqlDbType.Bit);
MySqlParam4.Value = "1";
MySqlParam4.SourceColumn = "contract";
adp.InsertCommand.Parameters.Add(MySqlParam4);
}
What the heck am I missing here?
Thanks.
private void CodedDataGrid_Load(object sender, System.EventArgs e)
{
//create a new connection object
String ConnectString = "Server=" + "MyMachine" + "; " + "User ID=" + "sa" + "; Password=" + "password" + "; initial catalog = " + "pubs" + ";";
conn = new SqlConnection(ConnectString); //create a new adapter
adp = new SqlDataAdapter("Select au_id,au_lname,au_fname,contract FROM authors",conn);
//creat ea new dataset
ds = new DataSet();
adp.Fill(ds);
this.dbGrid.DataSource = ds;
}
private void btnNew_Click(object sender, System.EventArgs e)
{
AdapterInsert();
//add a new record to the dataset, but first
try
{
adp.Update(ds,"Table");
}
catch (System.Data.SqlClient.SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AdapterInsert()
{
insComm = new SqlCommand();
adp.InsertCommand = insComm;
adp.InsertCommand.Connection = conn;
adp.InsertCommand.CommandType =
CommandType.StoredProcedure;
adp.InsertCommand.CommandText = "InsertAuthor";
SqlParameter MySqlParam1 = new SqlParameter("@au_id",SqlDbType.VarChar);
MySqlParam1.Value = this.txtID.Text;
MySqlParam1.SourceColumn = "au_id";
adp.InsertCommand.Parameters.Add(MySqlParam1);
SqlParameter MySqlParam2 = new SqlParameter("@au_fname",SqlDbType.VarChar);
MySqlParam2.Value = this.txtFName.Text;
MySqlParam2.SourceColumn = "au_fname";
adp.InsertCommand.Parameters.Add(MySqlParam2);
SqlParameter MySqlParam3 = new SqlParameter("@au_lname",SqlDbType.VarChar);
MySqlParam3.Value = this.txtLName.Text;
MySqlParam3.SourceColumn = "au_lname";
adp.InsertCommand.Parameters.Add(MySqlParam3);
SqlParameter MySqlParam4 = new SqlParameter("@contract",SqlDbType.Bit);
MySqlParam4.Value = "1";
MySqlParam4.SourceColumn = "contract";
adp.InsertCommand.Parameters.Add(MySqlParam4);
}
What the heck am I missing here?
Thanks.