Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 23rd, 2010, 10:31 AM
Authorized User
Join Date: Jun 2003
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default Need help with DataTable /and DataAdapter

I am creating datatable and adding rows in a loop commented as
"populate datatable"

I then try to use the datatable when I am adding parameters to a sql
command. I get the error message "no row at position 0"

I am new to datatables/adapters and am sure my syntax and indexing
is incorrect. Thanks in advance. - Steve

Protected Sub UpdateIssues()
'If get here there are 5 or less selected issues
'prepare dataset
Dim i As Integer
Dim Conn As New SqlConnection(SqlDataSource1.ConnectionString)

Dim adapterUpdateIssues As SqlDataAdapter
Dim IssuesDataTable As New DataTable
IssuesDataTable.Columns.Add("Issue", GetType(String))

' populate datatable

Dim SelectedItemIndex As Integer
SelectedItemIndex = 0
For i = 0 To CheckBoxListIssues.Items.Count - 1
If CheckBoxListIssues.Items(i).Selected = True Then
Dim IssueRow As DataRow = IssuesDataTable.NewRow
IssueRow("Issue") = CheckBoxListIssues.Items(i).Text
SelectedItemIndex = SelectedItemIndex + 1
End If
' datatable populated
Dim cmdUpdate As New SqlCommand("procUpdateIssues", Conn)

cmdUpdate.Parameters.AddWithValue("@Issue1", IssuesDataTable.Rows(0).ItemArray(0))
cmdUpdate.Parameters.AddWithValue("@Issue2", IssuesDataTable.Rows(1).ItemArray(0))
cmdUpdate.Parameters.AddWithValue("@Issue3", IssuesDataTable.Rows(2).ItemArray(0))
cmdUpdate.Parameters.AddWithValue("@Issue4", IssuesDataTable.Rows(3).ItemArray(0))
cmdUpdate.Parameters.AddWithValue("@Issue5", IssuesDataTable.Rows(4).ItemArray(0))
cmdUpdate.Parameters.AddWithValue("@adid", Session("adid"))

adapterUpdateIssues = New SqlDataAdapter(cmdUpdate)
adapterUpdateIssues.UpdateCommand = cmdUpdate

End Sub
  #2 (permalink)  
Old March 24th, 2010, 03:23 AM
Friend of Wrox
Points: 2,473, Level: 20
Points: 2,473, Level: 20 Points: 2,473, Level: 20 Points: 2,473, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: May 2004
Location: India
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts

please check the example for datatable

Public Class Form1 Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Dim Table1 As DataTable
Table1 = New DataTable("Customers")
'creating a table named Customers
Dim Row1, Row2, Row3 As DataRow
'declaring three rows for the table
Dim Name As DataColumn = New DataColumn("Name")
'declaring a column named Name
Name.DataType = System.Type.GetType("System.String")
'setting the datatype for the column
'adding the column to table
Dim Product As DataColumn = New DataColumn("Product")
Product.DataType = System.Type.GetType("System.String")
Dim Location As DataColumn = New DataColumn("Location")
Location.DataType = System.Type.GetType("System.String")

Row1 = Table1.NewRow()
'declaring a new row
Row1.Item("Name") = "Reddy"
'filling the row with values. Item property is used to set the field value.
Row1.Item("Product") = "[COLOR=#009900! important][COLOR=#009900! important]Notebook[/COLOR][/COLOR]"
'filling the row with values. adding a product
Row1.Item("Location") = "Sydney"
'filling the row with values. adding a location
'adding the completed row to the table
Row2 = Table1.NewRow()
Row2.Item("Name") = "Bella"
Row2.Item("Product") = "[COLOR=#009900! important][COLOR=#009900! important]Desktop[/COLOR][/COLOR]"
Row2.Item("Location") = "Adelaide"
Row3 = Table1.NewRow()
Row3.Item("Name") = "Adam"
Row3.Item("Product") = "[COLOR=#009900! important][COLOR=#009900! important]PDA[/COLOR][/COLOR]"
Row3.Item("Location") = "Brisbane"
End Try

Dim ds As New DataSet()
ds = New DataSet()
'creating a dataset
'adding the table to dataset 
DataGrid1.SetDataBinding(ds, "Customers")
'binding the table to datagrid
End Sub

End Class
Om Prakash Pant
Click the "Thanks" button if this post helped you.

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How many DataAdapter in a Form melvik ADO.NET 5 November 2nd, 2008 03:14 AM
update dataadapter karenai Visual Basic 2005 Basics 0 February 9th, 2008 10:46 PM
Dataadapter with parameter kau_shuk VS.NET 2002/2003 0 January 4th, 2007 02:43 AM
Datatable/DataView --> DataAdapter - ThX ! ank2go ADO.NET 4 April 28th, 2004 09:32 AM
updating db with dataAdapter/dataTable problem mm1234 ADO.NET 0 November 19th, 2003 06:49 AM

All times are GMT -4. The time now is 07:42 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.