467,222 Members | 1,457 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,222 developers. It's quick & easy.

C#+ Database Update Through The Datagrid View+ Windows Forms+ Very Urgent

hey friends , i m not being able to save the updates in datagrid view when i press buttonsave_ gridview i have a class customer with two methods one to get the dataset and other to update database:

i dont know what is the problem with my code.
i always get UPDATE syntax run time error, as i press button_save_grod view :
please help me.
my code goes like this:

Expand|Select|Wrap|Line Numbers
  1. public class customer
  2. {
  3.   // private static string strConn;
  4.  
  5.         private static DataTable ds;
  6.  
  7.  
  8.    public  DataTable getdataset(string fname, DataGridView dg)
  9.    {
  10.         OleDbConnection con = new OleDbConnection(@"provider= Microsoft.Jet.OLEDb.4.0; Data Source = D:\My Documents\Visual Studio 2005\Projects\db2.mdb");
  11.         con.Open();
  12.  
  13.         OleDbDataAdapter thisadapter1 = new OleDbDataAdapter("Select * from tbl_client where fname= '" + fname + "'", con);
  14.         //OleDbCommandBuilder cd = new OleDbCommandBuilder(thisadapter1);
  15.         ds = new DataTable();
  16.         thisadapter1.Fill(ds);
  17.  
  18.         dg.BringToFront();
  19.         dg.Visible = true;
  20.         BindingSource bsource = new BindingSource();
  21.         bsource.DataSource = ds;
  22.         dg.DataSource = bsource;
  23.         dg.Columns[0].Visible = false;
  24.         dg.Columns[1].Visible = false;
  25.         dg.Columns[2].HeaderText = "Fname";
  26.         dg.Columns[2].Width = 45;
  27.         dg.Columns[3].HeaderText = "Lname";
  28.         dg.Columns[3].Width = 45;
  29.         dg.Columns[5].HeaderText = "Gender";
  30.         dg.Columns[5].Width = 46;
  31.         dg.Columns[6].HeaderText = "Address";
  32.         dg.Columns[6].Width = 48;
  33.         dg.Columns[4].Visible = false;
  34.         dg.Columns[7].HeaderText = "Phone";
  35.         dg.Columns[7].Width = 45;
  36.         dg.Columns[8].HeaderText = "Mobile";
  37.         dg.Columns[8].Width = 45;
  38.         dg.Columns[9].HeaderText = "Email";
  39.         dg.Columns[9].Width = 45;
  40.         dg.Columns[10].HeaderText = "Project_ID";
  41.         dg.Columns[10].Width = 55;
  42.         dg.Columns[11].HeaderText = "Visit Date";
  43.         dg.Columns[11].Width = 55;
  44.         dg.Columns[12].HeaderText = "Agent";
  45.         dg.Columns[12].Width = 45;
  46.         dg.Columns[13].HeaderText = "FeedBack";
  47.         dg.Columns[14].HeaderText = "Remarks";
  48.         string feedback = "FeedBack";
  49.         dg.Columns[13].HeaderText = feedback;
  50.  
  51.         return ds;
  52.  
  53.    }
  54.  
  55.  
  56.   public int update()
  57.    {
  58.       OleDbConnection con = new OleDbConnection(@"provider= Microsoft.Jet.OLEDb.4.0; Data Source = D:\My Documents\Visual Studio 2005\Projects\db2.mdb");
  59.       con.Open();
  60.  
  61.      string sqlUpdate;
  62.       sqlUpdate = "Update tbl_client Set date= @p2, fname=@p3, lname= @p4, mname= @p5, gender= @p6, address= @p7, phone= @p8, mobile=@p9, email=@p10, project_id= @p11, visit_date= @p12, agent= @p13, feetdback1= @p14, remarks= @p15 Where client_id= '@p1'";
  63.       OleDbParameter[] pUpdate = new OleDbParameter[15];
  64.  
  65.  
  66.       pUpdate[0] = new OleDbParameter("@p1", OleDbType.VarChar, 40,"client_id");
  67.       pUpdate[1] = new OleDbParameter("@p2", OleDbType.DBDate, 30, "date");
  68.       pUpdate[2] = new OleDbParameter("@p3", OleDbType.VarChar, 40,"fname");
  69.       pUpdate[3] = new OleDbParameter("@p4", OleDbType.VarChar, 20,"lname");
  70.       pUpdate[4] = new OleDbParameter("@p5", OleDbType.VarChar, 20,"mname");
  71.       pUpdate[5] = new OleDbParameter("@p6", OleDbType.VarChar, 40, "gender");
  72.       pUpdate[6] = new OleDbParameter("@p7", OleDbType.VarChar, 40, "address");
  73.       pUpdate[7] = new OleDbParameter("@p8", OleDbType.Numeric, 9, "phone");
  74.       pUpdate[8] = new OleDbParameter("@p9", OleDbType.Numeric, 9, "mobile");
  75.       pUpdate[9] = new OleDbParameter("@p10", OleDbType.VarChar, 40, "email");
  76.       pUpdate[10] = new OleDbParameter("@p11", OleDbType.VarChar, 40, "project_id");
  77.       pUpdate[11] = new OleDbParameter("@p12", OleDbType.Date, 40, "visit_date");
  78.       pUpdate[12] = new OleDbParameter("@p13", OleDbType.VarChar, 40, "agent");
  79.       pUpdate[13] = new OleDbParameter("@p14", OleDbType.VarChar, 40, "feedback1");
  80.       pUpdate[14] = new OleDbParameter("@p15", OleDbType.VarChar, 30, "remarks");
  81.  
  82.       OleDbCommand cmdUpdate = new OleDbCommand(sqlUpdate,con);
  83.      // cmdUpdate.Parameters.AddRange(pUpdate);
  84.  
  85.  
  86.       OleDbDataAdapter da = new OleDbDataAdapter();
  87.       da.UpdateCommand  = cmdUpdate;
  88.       int i=da.Update(ds);
  89.       ds.AcceptChanges();
  90.        if(i>0)
  91.        {
  92.        MessageBox.Show("database updated");
  93.      }
  94.      return i;
  95.     }
  96.  
  97.  
  98.  }
thanks in advance
Radhika
Feb 24 '08 #1
  • viewed: 1473
Share:
1 Reply
kenobewan
Expert 4TB
This wouldn't have something to do with the client_id. Why is the parameter is sngle quotes and why isn't it an integer?
Feb 24 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by EMW | last post: by
3 posts views Thread by Bill C. | last post: by
3 posts views Thread by Ajay Krishnan Thampi | last post: by
11 posts views Thread by CM Manager via DotNetMonster.com | last post: by
5 posts views Thread by Stephen Plotnick | last post: by
9 posts views Thread by =?Utf-8?B?TUNN?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.