Acquiring Datagrid field info


May 30, 2005
Programming Experience
Hi all,

I have a web app with a Datagrid that gets populated from a database table. What I want to do is take the info from the row in my datagrid(I only populate one row) as well as the selected item in my radiobuttonlist which is under the datagrid and send all of this in a new table in my database on a button_click event. I just can't seem to be able to get the info out of the fields of my datagrid. Here is the code i'm using......Any help at all would be greatly appreciated since this is the 5th website were I posted my problem and still no answers...
Thanks alot in advance!

Private Sub btnaddrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddrow.Click

'Getting values from Datagrid

Dim dgi As DataGridItem

Dim Publisher As String = DropDownList1.SelectedItem.Text

Dim Title As String = dgi.Cells(0).Text

Dim First_Name As String = dgi.Cells(1).Text

Dim Last_Name As String = dgi.Cells(2).Text

Dim Address As String = dgi.Cells(4).Text

Dim City As String = dgi.Cells(5).Text

Dim Province As String = dgi.Cells(6).Text

Dim PostalCode As String = dgi.Cells(7).Text

Dim Barcode As String = dgi.Cells(8).Text

Dim Reason As String = RadioButtonList1.SelectedItem.Text

'Create some ADO.NET objects

Dim ds As DataSet = New DataSet

Dim da As SqlDataAdapter = New SqlDataAdapter

'Create a SlCommand to select data

Dim cmdSelect As SqlCommand = SqlConnection1.CreateCommand

cmdSelect.CommandType = CommandType.Text

cmdSelect.CommandText = "Select * from PubReturn"

'Create a SqlCommand to insert data

Dim cmdInsert As SqlCommand = SqlConnection1.CreateCommand

cmdInsert.CommandType = CommandType.Text

cmdInsert.CommandText = "INSERT INTO PubReturn " & "(Publisher, Title, First_Name, Last_Name, Address, City, Province, PostalCode, Barcode, Reason)" & "Values(@Publisher, @Title, @First_Name, @Last_Name, @Address, @City, @Province, @PostalCode, @Barcode, " & "@Reason)"

'Sete up the DataAdapter and fill the Dataset

da.SelectCommand = cmdSelect

da.InsertCommand = cmdInsert

da.Fill(ds, "PubReturn")

'Create a new Datarow

Dim dr As DataRow = ds.Tables("PubReturn").NewRow

'Set values

dr(0) = Publisher.ToString

dr(1) = Title.ToString

dr(2) = First_Name.ToString

dr(3) = Last_Name.ToString

dr(4) = Address.ToString

dr(5) = City.ToString

dr(6) = Province.ToString

dr(7) = PostalCode.ToString

dr(8) = Barcode.ToString

dr(9) = Reason.ToString

'And append the new row to the DataTable


'Now save back to the database

da.Update(ds, "PubReturn")

End Sub

I hope someone can help me out here... !!!!
Later guys and girls!!


Mar 11, 2005
Programming Experience
Dim dgi As DataGridItem
Dim Publisher AsString = DropDownList1.SelectedItem.Text
Dim Title AsString = dgi.Cells(0).Text
well, well,

1- Dgi is not instantiated yet.
2- Assuming that you instantiate dgi did you assign any data to the dgi yet?
3- Try to insert some debug (messagebox statements) to chekc intermediate results. I see this is an ASP.NET application you are doing right?
Top Bottom