c# sql update question

S

Starbuck

Hi

The code below saves data from my edit screen, it shows no errors and seems
to run but nothing is written back to the table,
Can anyone spot my mistake
Thanks


private void updateRec()

{

try

{

SqlConnection con = new SqlConnection("Data Source=10.0.0.3;Initial
Catalog=maptrak;uid="+globals.bc.UID+";Password="+globals.bc.PWD+";");


SqlDataAdapter sqlDa = new SqlDataAdapter("select * from bases", con);

DataSet dSet = new DataSet();

sqlDa.Fill(dSet, "bases");

string sqlUpString = "update bases set Date_Modified=@Date_Modified,
Name=@Name," +

"Addr1=@Addr1,Addr2=@Addr2,Addr3=@Addr3,Addr4=@Addr4," +

"Postcode=@Postcode,contact=@contact,telephone=@telephone," +

"fax=@fax,mobile_tel=@mobile_tel,email=@email,conNumber=@conNumber" +

"notes=@notes,maplat=@maplat,maplon=@maplon,icon=@icon,showonmap=@showonmap"
+

"where accref=@Accref";

SqlCommand upCmd = new SqlCommand(sqlUpString,con);

upCmd.Parameters.Add("@Accref", SqlDbType.NChar,8,ref_txt.Text);

upCmd.Parameters.Add("@Date_Modified",
SqlDbType.DateTime,8,DateTime.Now.ToString());

upCmd.Parameters.Add("@Name", SqlDbType.NChar,50,Name_txt.Text);

upCmd.Parameters.Add("@Addr1", SqlDbType.NChar,50,Address_txt0.Text);

upCmd.Parameters.Add("@Addr2", SqlDbType.NChar,50,Address_txt1.Text);

upCmd.Parameters.Add("@Addr3", SqlDbType.NChar,50,Address_txt2.Text);

upCmd.Parameters.Add("@Addr4", SqlDbType.NChar,50,Address_txt3.Text);

upCmd.Parameters.Add("@Postcode", SqlDbType.NChar,50,Postcode_txt.Text);

upCmd.Parameters.Add("@contact", SqlDbType.NChar,50,Contact_txt.Text);

upCmd.Parameters.Add("@telephone", SqlDbType.NChar,50,Phone_txt0.Text);

upCmd.Parameters.Add("@fax", SqlDbType.NChar,50,Phone_txt1.Text);

upCmd.Parameters.Add("@mobile_tel", SqlDbType.NChar,50,Phone_txt2.Text);

upCmd.Parameters.Add("@email", SqlDbType.NChar,50,email_txt.Text);

upCmd.Parameters.Add("@conNumber", SqlDbType.NChar,50,message_txt.Text);

upCmd.Parameters.Add("@notes", SqlDbType.NChar,250,notes_txt.Text);

upCmd.Parameters.Add("@maplat", SqlDbType.Float,8,mapLat.Text);

upCmd.Parameters.Add("@maplon", SqlDbType.Float,8,maplon.Text);

upCmd.Parameters.Add("@icon", SqlDbType.Int,4, icon_txt.Text);

if (mapChk.Checked==true)

{

upCmd.Parameters.Add("@showonmap", SqlDbType.Int,4,"1");

}

else

{

upCmd.Parameters.Add("@showonmap", SqlDbType.Int,4,"0");

}

sqlDa.UpdateCommand = upCmd;

sqlDa.Update(dSet,"bases");


}

catch (Exception err)

{

MessageBox.Show(err.Message);

}

}
 
A

Anders Borum

Hello!

It seems like you aren't making any changes to the retrieved DataSet. For
the UpdataCommand to affect any rows, you need to change atleast a single
column on a single row before invoking the Update method on the DataAdapter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top