This is a multi-part message in MIME format.
------=_NextPart_000_000A_01C470C1.0AB15790
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
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 =3D "Server=3D" + "MyMachine" + "; " + "User ID=3D" =
+ "sa" + "; Password=3D" + "password" + "; initial catalog =3D " + =
"pubs" + ";";
conn =3D new SqlConnection(ConnectString); //create a new adapter
adp =3D new SqlDataAdapter("Select au_id,au_lname,au_fname,contract FROM =
authors",conn);
//creat ea new dataset
ds =3D new DataSet();
adp.Fill(ds);
this.dbGrid.DataSource =3D ds;
}
private void btnNew_Click(object sender, System.EventArgs e)
{
AdapterInsert();
=20
//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 =3D new SqlCommand();
adp.InsertCommand =3D insComm;
adp.InsertCommand.Connection =3D conn;
adp.InsertCommand.CommandType =3D=20
CommandType.StoredProcedure;
adp.InsertCommand.CommandText =3D "InsertAuthor";
SqlParameter MySqlParam1 =3D new =
SqlParameter("@au_id",SqlDbType.VarChar);
MySqlParam1.Value =3D this.txtID.Text;
MySqlParam1.SourceColumn =3D "au_id";
adp.InsertCommand.Parameters.Add(MySqlParam1);
SqlParameter MySqlParam2 =3D new =
SqlParameter("@au_fname",SqlDbType.VarChar);
MySqlParam2.Value =3D this.txtFName.Text;
MySqlParam2.SourceColumn =3D "au_fname";
adp.InsertCommand.Parameters.Add(MySqlParam2);
SqlParameter MySqlParam3 =3D new =
SqlParameter("@au_lname",SqlDbType.VarChar);
MySqlParam3.Value =3D this.txtLName.Text;
MySqlParam3.SourceColumn =3D "au_lname";
adp.InsertCommand.Parameters.Add(MySqlParam3);
SqlParameter MySqlParam4 =3D new =
SqlParameter("@contract",SqlDbType.Bit);
MySqlParam4.Value =3D "1";
MySqlParam4.SourceColumn =3D "contract";
adp.InsertCommand.Parameters.Add(MySqlParam4);
}
What the heck am I missing here?
Thanks.
=20
------=_NextPart_000_000A_01C470C1.0AB15790
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2162" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>I am pulling my hair out over this one. =
I have done=20
this same kind of thing in VB.Net but in C# it doesn't work. I dont' =
receive any=20
errors either, it just won't update the pubs.Authors table. Here is the=20
code:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>private void CodedDataGrid_Load(object =
sender,=20
System.EventArgs e)<BR> {<BR> //create a new =
connection=20
object<BR></FONT><FONT face=3DArial size=3D2>String ConnectString =3D =
"Server=3D" +=20
"MyMachine" + "; " + </FONT><FONT face=3DArial size=3D2>"User ID=3D" + =
"sa" + ";=20
Password=3D" + "password" + </FONT><FONT face=3DArial size=3D2>"; =
initial catalog =3D "=20
+ "pubs" + ";";<BR></FONT><FONT face=3DArial size=3D2>conn =3D new=20
SqlConnection(ConnectString);</FONT><FONT face=3DArial=20
size=3D2> //create a new adapter<BR>adp =3D new=20
SqlDataAdapter("Select </FONT><FONT face=3DArial=20
size=3D2>au_id,au_lname,au_fname,contract FROM =
authors",conn);<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2> //creat ea new=20
dataset<BR> ds =3D new=20
DataSet();<BR> adp.Fill(ds);<BR> this.d=
bGrid.DataSource=20
=3D ds;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> }</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> private void =
btnNew_Click(object=20
sender, System.EventArgs=20
e)<BR> {<BR> AdapterInsert();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial =
size=3D2> <BR> //add a new=20
record to the dataset, but first</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2> try<BR> {<BR> &nbs=
p; adp.Update(ds,"Table");<BR> }<BR> &nb=
sp;catch=20
(System.Data.SqlClient.SqlException=20
sqlEx)<BR> {<BR> MessageBox.Show(=
sqlEx.Message);<BR> }<BR> catch(System.=
Exception=20
ex)<BR> {<BR> MessageBox.Show(ex.=
Message);<BR> }</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> }<BR> private =
void=20
AdapterInsert()<BR> {<BR> insComm =3D new=20
SqlCommand();<BR> adp.InsertCommand =3D=20
insComm;<BR> adp.InsertCommand.Connection =3D =
conn;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial =
size=3D2> adp.InsertCommand.CommandType =3D=20
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2>CommandType.StoredProcedure;<BR> adp.InsertComm=
and.CommandText=20
=3D "InsertAuthor";</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> SqlParameter =
MySqlParam1 =3D new=20
</FONT><FONT face=3DArial=20
size=3D2>SqlParameter("@au_id",SqlDbType.VarChar);<BR> M=
ySqlParam1.Value=20
=3D this.txtID.Text;<BR> MySqlParam1.SourceColumn =3D=20
"au_id";<BR> adp.InsertCommand.Parameters.Add(MySqlParam=
1);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> SqlParameter =
MySqlParam2 =3D new=20
</FONT><FONT face=3DArial=20
size=3D2>SqlParameter("@au_fname",SqlDbType.VarChar);<BR> &nbs=
p;MySqlParam2.Value=20
=3D this.txtFName.Text;<BR> MySqlParam2.SourceColumn =
=3D=20
"au_fname";<BR> adp.InsertCommand.Parameters.Add(MySqlPa=
ram2);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> SqlParameter =
MySqlParam3 =3D new=20
</FONT><FONT face=3DArial=20
size=3D2>SqlParameter("@au_lname",SqlDbType.VarChar);<BR> &nbs=
p;MySqlParam3.Value=20
=3D this.txtLName.Text;<BR> MySqlParam3.SourceColumn =
=3D=20
"au_lname";<BR> adp.InsertCommand.Parameters.Add(MySqlPa=
ram3);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> SqlParameter =
MySqlParam4 =3D new=20
</FONT><FONT face=3DArial=20
size=3D2>SqlParameter("@contract",SqlDbType.Bit);<BR> My=
SqlParam4.Value=20
=3D "1";<BR> MySqlParam4.SourceColumn =3D=20
"contract";<BR> adp.InsertCommand.Parameters.Add(MySqlPa=
ram4);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>}<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>What the heck am I missing =
here?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Thanks.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> </DIV></FONT>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV></BODY></HTML>
------=_NextPart_000_000A_01C470C1.0AB15790--