473,399 Members | 4,254 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,399 software developers and data experts.

How to delete row from datagridview and update database?

Hey guys,

I have written a code that could delete selected row in data grid view. I gives lots of errors.

Let me paste my code.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < dataGridView1.Rows.Count; i++)
  2.                     {
  3.  
  4.                         DataGridViewRow dr = dataGridView1.Rows[i];
  5.                         if (dr.Selected == true)
  6.                         {
  7.                             dataGridView1.Rows.RemoveAt(i);
  8.  
  9.  
  10.                             cmd.CommandText = "Delete from transact1 where C_ID ='" + i + "'";
  11.                             cmd.ExecuteNonQuery();
  12.  
  13.                             da.Update(ds, "transact1");
  14.                             MessageBox.Show("Deleted");
  15.                         }
  16.  
  17.                         con1.Close();
  18.                     }

While deleting row it throws exception.
"Connection property not initialized"

I have tried opening connection in different ways but it didn't help either. So can anyone tell me what can possibly wrong with this code?

Thanks
Ajinkya
Mar 5 '11 #1
1 14258
Leito
58
What code do you use to open the connection to the database?

If your database is hosted on an SQL server, you should use something like this:
Expand|Select|Wrap|Line Numbers
  1. String strConnection = "Data Source=SQLSERVERNAME; Initial Catalog=DATABASENAME; User Id=USERNAME; Password=USERPASSWORD;Connection Timeout=30;"
  2. SqlConnection cn = new SqlConnection(strConnection);
  3. cn.Open();
Mar 7 '11 #2

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

Similar topics

2
by: tamimi | last post by:
I have a guestbook form that inserts the fields into an Access Database. The database is residing in the "fpdb" folder that was created by FrontPage. The ASP page that writes the data into the...
1
by: Fie Fie Niles | last post by:
I have IIS installed on XP Professional workstation machine. I have an ASP page that open connection to an Access database, then when trying to update the database, it gave me the error "cannot...
1
by: Darn | last post by:
Hi all How do i solve this problem. I'm web developer from Malaysia, the problem occurs when i ftp an Access database which i'm using for this particular web site for it's database.The form...
13
by: =?Utf-8?B?VmVybm9uIFBlcHBlcnM=?= | last post by:
I am using VS2005. I created a Windows Appication project. Inside the Server Explorer, I created a new SQLExpress database, and then created a new table. I added the rows, making my first row an...
4
by: cyrusjo | last post by:
Hello Ones, I am doin a small project in C# and MS access, in which i have to do the insert delete and update operations, can anyone tell me the syntax of the above said commands.I am using...
0
by: tom | last post by:
When I try to read in a csv file it gives me this error message. 'Cannot update. Database or object is read-only.' If I change the extension to txt it processes just fine. I have googled all...
1
by: dillipdhal | last post by:
Hi This is Dillip from HSBC Software,Hyd. Now I m working in a financial domain. Please help me in "How can I insert data from a datagridview to database in vb .net and also retrive the data "
6
by: Tark Siala | last post by:
hi i'm using VS.NET 2005 +SP1 C#, and i connect access database by ADO.NET 2.0. i'm using Dataset to (insert,update,delete) and in get data i use (Datareader), but my problem when i do this: 1 -...
1
by: MFayaz | last post by:
Hello! I have to refresh datagridview when database update , IS there any way to solution that my datagridview auto refresh when any user update database. Thanks in Advance
1
by: adsaca | last post by:
here's my code in filling a datagridview,which i retrieve from database mysql, dim sql as String sql = "SELECT * FROM table_user" Dim myData As New DataTable ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.