473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

c#+ update database according to datagrid view

i have a table tbl_client in Ms Access database. my program displays a datagrid view with the search result records. i want to save the changes to data gridview to database.for this i call the update method of a class customer to save the changes when user click on update button. every time i run the code i get the run time error datatype mis match error in update statement.
my class goes like this:

Expand|Select|Wrap|Line Numbers
  1. public int update()
  2.    {
  3.       OleDbConnection con = new OleDbConnection(@"provider= Microsoft.Jet.OLEDb.4.0; Data Source = D:\My Documents\Visual Studio 2005\Projects\db2.mdb");
  4.       con.Open();
  5.  
  6.       string sqlUpdate;
  7.  
  8.       sqlUpdate = " Update tbl_client Set fname= '@p1' Where client_id='@p0'";      
  9.      // sqlUpdate = "Update tbl_client Set reg_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', feedback1= '@p14', remarks= '@p15' Where client_id= '@p1'";
  10.       OleDbParameter[] pUpdate = new OleDbParameter[15];
  11.  
  12.  
  13.       pUpdate[0] = new OleDbParameter("@p0", OleDbType.Integer, 40,"client_id");
  14.       pUpdate[1] = new OleDbParameter("@p1", OleDbType.VarChar, 20, "fname");
  15.       pUpdate[1] = new OleDbParameter("@p2", OleDbType.Date, 30, "reg_date");
  16.       pUpdate[2] = new OleDbParameter("@p3", OleDbType.VarChar, 40,"fname");
  17.       pUpdate[3] = new OleDbParameter("@p4", OleDbType.VarChar, 20,"lname");
  18.       pUpdate[4] = new OleDbParameter("@p5", OleDbType.VarChar, 20,"mname");
  19.       pUpdate[5] = new OleDbParameter("@p6", OleDbType.VarChar, 40, "gender");
  20.       pUpdate[6] = new OleDbParameter("@p7", OleDbType.VarChar, 40, "address");
  21.       pUpdate[7] = new OleDbParameter("@p8", OleDbType.Integer, 9, "phone");
  22.       pUpdate[8] = new OleDbParameter("@p9", OleDbType.Integer, 9, "mobile");
  23.       pUpdate[9] = new OleDbParameter("@p10", OleDbType.VarChar, 40, "email");
  24.       pUpdate[10] = new OleDbParameter("@p11", OleDbType.VarChar, 40, "project_id");
  25.       pUpdate[11] = new OleDbParameter("@p12", OleDbType.Date, 40, "visit_date");
  26.       pUpdate[12] = new OleDbParameter("@p13", OleDbType.VarChar, 40, "agent");
  27.       pUpdate[13] = new OleDbParameter("@p14", OleDbType.LongVarChar, 40, "feedback1");
  28.       pUpdate[14] = new OleDbParameter("@p15", OleDbType.LongVarChar, 30, "remarks");
  29.  
  30.       OleDbCommand cmdUpdate = new OleDbCommand(sqlUpdate,con);
  31.  
  32.      // cmdUpdate.Parameters.AddRange(pUpdate);
  33.  
  34.  
  35.       OleDbDataAdapter da = new OleDbDataAdapter();
  36.       OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
  37.      // DataSet ds = new DataSet();
  38.  
  39.       da.UpdateCommand  = cmdUpdate;
  40.       int  i=da.Update(ds);
  41.       ds.AcceptChanges();
  42.       if (i > 0)
  43.       {
  44.           MessageBox.Show("database updated");
  45.       }
  46.       else {
  47.           MessageBox.Show("update error.");
  48.       }
  49.      return i;
  50.  
  51.     }
what is the problem with my update method.

thanks in advance
Radhika
Mar 6 '08 #1
1 1516
kenobewan
4,871 Expert 4TB
Let's start with the update query, if you have the name of the parameter in single quotes its a string...
Mar 6 '08 #2

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

Similar topics

2
by: nss | last post by:
hi, I created web page ( .aspx) then i imported datagrid on that page with dataset. Using OleDbDataAdapter i made select qurey when page is launch it shows row. But i wanna update and insert new...
5
by: jason | last post by:
Hi, all How can I update data (multiple rows, but not every rows) using dataset in datagrid? I mean is there any way I can let datagrid know which row(s)/column(s) has been modified and update...
0
by: Tony A. | last post by:
I'm using VB 2005 with MS Access for as the database. I have a table (tblItem) that has two columns, ItemNum (key field) and ItemDescription. I also have a second table (tblOrdersDetail), this...
1
by: farid25 | last post by:
update data in datagrid asp vb.net -------------------------------------------------------------------------------- Hi, all i got data base contain 3 tables t_students student_id ...
1
by: dhureya | last post by:
I Have A Problem In Updating The Database Using The Datagrid Control In Vb.net.i Need To Update,insert Or Delete The Data From The Datagrid To Database. Please Reply Me As Soon As...
4
by: Rohith | last post by:
I m not able to update Database through DataGrid.I am working in VB.Net .I can retrieve the Data but could not update the data through datagrid.Please help me.
1
by: radhikabista | last post by:
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:...
2
tjc0ol
by: tjc0ol | last post by:
Hi guys, Im just wondering that I cannot update my database throught datagrid datalist control, and the only thing works is that I can delete, cancel, edit but when I clicked update link...
0
by: menyki | last post by:
pls i designed a database application using visual basic. i have done the coding for the print incase i want to print out the data. Pls my problem is how to code at design time for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.