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.

Problem in adding Multiple rows in a datagridview using dataAdapter

I am using dataadapter to Add,Update,Delete rows in a datagridview.Update 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 SqlCommandBuilder = New SqlCommandBuilder(adapterGetBudget)

Dim dtdate As Date = Date.Now

Dim cmd As New SqlCommand()

With cmd

.CommandType = CommandType.StoredProcedure

.CommandText = "sp_AddDetailsBudget"

.Connection = Conn

.Parameters.Add("@User", SqlDbType.VarChar).Value = currUserName.ToString

.Parameters.Add("@Date", SqlDbType.SmallDateTime).Value = dtdate.Date.ToString

.Parameters.Add("@Time", SqlDbType.VarChar).Value = dtdate.ToShortTimeString.ToString

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

.Parameters.Add("Description", SqlDbType.VarChar).Value = txtBudgetDesc.Text.Trim

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

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

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

.Parameters.Add("@CostCenter", SqlDbType.VarChar).Value = Me.dgbudgetdet.Rows(0).Cells(3).Value

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

End With

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

Conn.Open()

adapterGetBudget.InsertCommand = cmd

If (ds.HasChanges) Then
adapterGetBudget.InsertCommand
adapterGetBudget.UpdateCommand = myBuilder.GetUpdateCommand()
adapterGetBudget.DeleteCommand = myBuilder.GetDeleteCommand()
adapterGetBudget.Update(ds)
End If
ds.AcceptChanges()

Thanks in Advance
Jun 26 '07 #1
2 4592
nateraaaa
663 Expert 512MB
I am using dataadapter to Add,Update,Delete rows in a datagridview.Update 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 SqlCommandBuilder = New SqlCommandBuilder(adapterGetBudget)

Dim dtdate As Date = Date.Now

Dim cmd As New SqlCommand()

With cmd

.CommandType = CommandType.StoredProcedure

.CommandText = "sp_AddDetailsBudget"

.Connection = Conn

.Parameters.Add("@User", SqlDbType.VarChar).Value = currUserName.ToString

.Parameters.Add("@Date", SqlDbType.SmallDateTime).Value = dtdate.Date.ToString

.Parameters.Add("@Time", SqlDbType.VarChar).Value = dtdate.ToShortTimeString.ToString

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

.Parameters.Add("Description", SqlDbType.VarChar).Value = txtBudgetDesc.Text.Trim

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

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

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

.Parameters.Add("@CostCenter", SqlDbType.VarChar).Value = Me.dgbudgetdet.Rows(0).Cells(3).Value

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

End With

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

Conn.Open()

adapterGetBudget.InsertCommand = cmd

If (ds.HasChanges) Then
adapterGetBudget.InsertCommand
adapterGetBudget.UpdateCommand = myBuilder.GetUpdateCommand()
adapterGetBudget.DeleteCommand = myBuilder.GetDeleteCommand()
adapterGetBudget.Update(ds)
End If
ds.AcceptChanges()

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
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 SqlCommandBuilder = New SqlCommandBuilder(adapterGetBudget)
If (ds.HasChanges(DataRowState.Added)) Then
Dim rowcount As Integer
Dim ds_changes As DataSet = ds.GetChanges(DataRowState.Added)
rowcount = ds_changes.Tables(0).Rows.Count
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.StoredProcedure
.CommandText = "sp_AddDetailsBudget"
.Connection = Conn
.Parameters.Add("@User", SqlDbType.VarChar).Value = currUserName.ToString
.Parameters.Add("@Date", SqlDbType.SmallDateTime).Value = dtdate.Date.ToString
.Parameters.Add("@Time", SqlDbType.VarChar).Value = dtdate.ToShortTimeString.ToString
.Parameters.Add("@Lock", SqlDbType.Bit).Value = 0
.Parameters.Add("Description", SqlDbType.VarChar).Value = txtBudgetDesc.Text.Trim
.Parameters.Add("@BudgetID", SqlDbType.BigInt).Value = Me.txtBudgetID.Text
.Parameters.Add("@Amount", SqlDbType.Real).Value = Me.dgbudgetdet.Rows(i).Cells(1).Value
.Parameters.Add("@AccountID", SqlDbType.BigInt).Value = Me.dgbudgetdet.Rows(i).Cells(2).Value
.Parameters.Add("@CostCenter", SqlDbType.VarChar).Value = Me.dgbudgetdet.Rows(i).Cells(3).Value
.Parameters.Add("@Notes", SqlDbType.VarChar).Value = Me.dgbudgetdet.Rows(i).Cells(4).Value
End With
If Conn.State = ConnectionState.Open Then Conn.Close()
Conn.Open()
adapterGetBudget.InsertCommand = cmd
adapterGetBudget.Update(ds_changes)
Next

ds.AcceptChanges()
Jun 27 '07 #3

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

Similar topics

2
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,...
11
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...
2
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...
4
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...
3
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...
7
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...
6
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...
6
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.