473,856 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem in adding Multiple rows in a datagridview using dataAdapter

4 New Member
I am using dataadapter to Add,Update,Dele te rows in a datagridview.Up date and delete are working but while adding a rows the problem comes.When I add more than one row only first row values are copied all other rows. Can anybody help me what to do.
Dim myBuilder As SqlCommandBuild er = New SqlCommandBuild er(adapterGetBu dget)

Dim dtdate As Date = Date.Now

Dim cmd As New SqlCommand()

With cmd

.CommandType = CommandType.Sto redProcedure

.CommandText = "sp_AddDetailsB udget"

.Connection = Conn

.Parameters.Add ("@User", SqlDbType.VarCh ar).Value = currUserName.To String

.Parameters.Add ("@Date", SqlDbType.Small DateTime).Value = dtdate.Date.ToS tring

.Parameters.Add ("@Time", SqlDbType.VarCh ar).Value = dtdate.ToShortT imeString.ToStr ing

.Parameters.Add ("@Lock", SqlDbType.Bit). Value = 0

.Parameters.Add ("Descriptio n", SqlDbType.VarCh ar).Value = txtBudgetDesc.T ext.Trim

.Parameters.Add ("@BudgetID" , SqlDbType.BigIn t).Value = Me.txtBudgetID. Text

.Parameters.Add ("@Amount", SqlDbType.Real) .Value = Me.dgbudgetdet. Rows(0).Cells(1 ).Value

.Parameters.Add ("@AccountID ", SqlDbType.BigIn t).Value = Me.dgbudgetdet. Rows(0).Cells(2 ).Value

.Parameters.Add ("@CostCente r", SqlDbType.VarCh ar).Value = Me.dgbudgetdet. Rows(0).Cells(3 ).Value

.Parameters.Add ("@Notes", SqlDbType.VarCh ar).Value = Me.dgbudgetdet. Rows(0).Cells(4 ).Value

End With

If Conn.State = ConnectionState .Open Then Conn.Close()

Conn.Open()

adapterGetBudge t.InsertCommand = cmd

If (ds.HasChanges) Then
adapterGetBudge t.InsertCommand
adapterGetBudge t.UpdateCommand = myBuilder.GetUp dateCommand()
adapterGetBudge t.DeleteCommand = myBuilder.GetDe leteCommand()
adapterGetBudge t.Update(ds)
End If
ds.AcceptChange s()

Thanks in Advance
Jun 26 '07 #1
2 4614
nateraaaa
663 Recognized Expert Contributor
I am using dataadapter to Add,Update,Dele te rows in a datagridview.Up date and delete are working but while adding a rows the problem comes.When I add more than one row only first row values are copied all other rows. Can anybody help me what to do.
Dim myBuilder As SqlCommandBuild er = New SqlCommandBuild er(adapterGetBu dget)

Dim dtdate As Date = Date.Now

Dim cmd As New SqlCommand()

With cmd

.CommandType = CommandType.Sto redProcedure

.CommandText = "sp_AddDetailsB udget"

.Connection = Conn

.Parameters.Add ("@User", SqlDbType.VarCh ar).Value = currUserName.To String

.Parameters.Add ("@Date", SqlDbType.Small DateTime).Value = dtdate.Date.ToS tring

.Parameters.Add ("@Time", SqlDbType.VarCh ar).Value = dtdate.ToShortT imeString.ToStr ing

.Parameters.Add ("@Lock", SqlDbType.Bit). Value = 0

.Parameters.Add ("Descriptio n", SqlDbType.VarCh ar).Value = txtBudgetDesc.T ext.Trim

.Parameters.Add ("@BudgetID" , SqlDbType.BigIn t).Value = Me.txtBudgetID. Text

.Parameters.Add ("@Amount", SqlDbType.Real) .Value = Me.dgbudgetdet. Rows(0).Cells(1 ).Value

.Parameters.Add ("@AccountID ", SqlDbType.BigIn t).Value = Me.dgbudgetdet. Rows(0).Cells(2 ).Value

.Parameters.Add ("@CostCente r", SqlDbType.VarCh ar).Value = Me.dgbudgetdet. Rows(0).Cells(3 ).Value

.Parameters.Add ("@Notes", SqlDbType.VarCh ar).Value = Me.dgbudgetdet. Rows(0).Cells(4 ).Value

End With

If Conn.State = ConnectionState .Open Then Conn.Close()

Conn.Open()

adapterGetBudge t.InsertCommand = cmd

If (ds.HasChanges) Then
adapterGetBudge t.InsertCommand
adapterGetBudge t.UpdateCommand = myBuilder.GetUp dateCommand()
adapterGetBudge t.DeleteCommand = myBuilder.GetDe leteCommand()
adapterGetBudge t.Update(ds)
End If
ds.AcceptChange s()

Thanks in Advance
Are you changing the parameters passed to the Insert proc for each row you are trying to add? It sounds like you keep passing the same parameters for each row. Try looping through the rows you want to add and setting the parameters with the row[columnname].Value for each parameter. Let us know if this works for you.

Nathan
Jun 26 '07 #2
mdfayazi
4 New Member
Thanks Me nateraaaa . I understood that I am passing only first row parameters . I tried your suggestion by keeping parameters in loop but the result is same. this is my code below . I have to make any changes.Please suggest me what changes.
Dim myBuilder As SqlCommandBuild er = New SqlCommandBuild er(adapterGetBu dget)
If (ds.HasChanges( DataRowState.Ad ded)) Then
Dim rowcount As Integer
Dim ds_changes As DataSet = ds.GetChanges(D ataRowState.Add ed)
rowcount = ds_changes.Tabl es(0).Rows.Coun t
Dim i As Integer
For i = 0 To rowcount - 1
Dim dtdate As Date = Date.Now
Dim cmd As New SqlCommand()
With cmd
.CommandType = CommandType.Sto redProcedure
.CommandText = "sp_AddDetailsB udget"
.Connection = Conn
.Parameters.Add ("@User", SqlDbType.VarCh ar).Value = currUserName.To String
.Parameters.Add ("@Date", SqlDbType.Small DateTime).Value = dtdate.Date.ToS tring
.Parameters.Add ("@Time", SqlDbType.VarCh ar).Value = dtdate.ToShortT imeString.ToStr ing
.Parameters.Add ("@Lock", SqlDbType.Bit). Value = 0
.Parameters.Add ("Descriptio n", SqlDbType.VarCh ar).Value = txtBudgetDesc.T ext.Trim
.Parameters.Add ("@BudgetID" , SqlDbType.BigIn t).Value = Me.txtBudgetID. Text
.Parameters.Add ("@Amount", SqlDbType.Real) .Value = Me.dgbudgetdet. Rows(i).Cells(1 ).Value
.Parameters.Add ("@AccountID ", SqlDbType.BigIn t).Value = Me.dgbudgetdet. Rows(i).Cells(2 ).Value
.Parameters.Add ("@CostCente r", SqlDbType.VarCh ar).Value = Me.dgbudgetdet. Rows(i).Cells(3 ).Value
.Parameters.Add ("@Notes", SqlDbType.VarCh ar).Value = Me.dgbudgetdet. Rows(i).Cells(4 ).Value
End With
If Conn.State = ConnectionState .Open Then Conn.Close()
Conn.Open()
adapterGetBudge t.InsertCommand = cmd
adapterGetBudge t.Update(ds_cha nges)
Next

ds.AcceptChange s()
Jun 27 '07 #3

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

Similar topics

2
5668
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow, DataTable.Rows.Add and DataAdapter.Update member functions. Before adding the new row object to the Rows collection, all of the row's fields that do not accept NULL must be assigned, otherwise an exception is thrown. However, I do not want to assign...
11
2261
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to reflect that the 3 items have been deleted only to discover that the 3 items appear, however when I click on them to display their information which runs a datareader over the same database it appears that the data has now gone. I wondered whether...
2
4701
by: Martin | last post by:
Hi all, This is the situation... DatagridView Control with datasource set to datatable. I want the user to be able to delete x number of rows by selecting a row with mouse then clicking a 'delete' button. Then user clicks a 'Commit' button to update the database.
4
18714
by: shibeta | last post by:
Hello, I have problem with DataGridView and BindingSource. I have created DataSet and added TableData to it with manualy created columns (without DataAdapter - I'm not using MSSQL). On the Form I put DataGridView and BindingSource, and connected them (BindingSource of course is also connected with DataSet). Next on the separated thread I'm reading data from DB and load them do DataSet (more preciselly, readed data I've saved as XML and...
3
2417
by: G .Net | last post by:
Hi Everybody I was wondering if anybody could help me with something that has been bothering me for some time. Basically, it is a problem I encountered some time ago with DataSets. I found a solution (which I will refer to later) but I have a gut feeling that there must be an easier way to do it. In order I can explain the problem, consider the following: Suppose I have a DataSet which has two tables "Customers" and "Orders". I set up...
7
15668
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is there an equivalent property for the DataGridView? I have searched, but have not found one. I would like the user to be able to see all the columns of the table on one screen - thus eliminating the need to use the horizontal scroll bar to view...
6
8467
by: =?Utf-8?B?TU1TSkVE?= | last post by:
How to let user delete multi rows from the BindingSource while the SelectionMode Property set to RowHeaderSelect I have in my program datagridview bound it to sql table Throw Bindingsource To fill it: MyTableTableAdaptor.fill(MyDataset.Mytable);
6
1916
by: =?ISO-8859-1?Q?Andrea_M=FCller?= | last post by:
Hello, I have one problem. Include my project is a dataset with tables. During my process I create a new temporary table. The dataset use my DataGrid Control. The use delete now one, two or more rows. After that the operator press <saveand I create the new table with
3
9705
by: jehugaleahsa | last post by:
Hello: I am binding a DataGridView with a BindingList<T>, where T is a custom business object that implements INotifyPropertyChanged. When you bind a DataGridView to a DataTable, it has this cool little feature - it will not call DataTable.Rows.Add until after you leave the DataGridView row. This is cool because it lets your user edit the record as much as needed to get it into a valid state before actually adding it to the DataTable.
0
9918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11060
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10786
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9532
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5762
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5959
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4578
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.