Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Databind to a Dataset

Status
Not open for further replies.

writedom

Programmer
Jul 26, 2003
8
US
I want to add the ability to display only those records entered into the temporary dataset (sdTemp) with a datagrid. Does anyone have any ideas? So far no one has been able to answer this for me.
Any Help greatly appreciated.



Dim objConnection As SqlConnection
Dim daNorthwind As SqlDataAdapter
Dim dsNorthwind As DataSet

Sub Page_Load(Source As Object, E As EventArgs)

Dim strConnection As String = ConfigurationSettings.AppSettings("NWind")
objConnection = New SqlConnection(strConnection)

Dim strSQL As String = "SELECT * FROM Categories"
daNorthwind = New SqlDataAdapter(strSQL, objConnection)

' Create a command builder object in order to create
' INSERT, UPDATE, and DELETE SQL statements automatically
Dim cb As New SqlCommandBuilder(daNorthwind)

' Is the page being loaded for the first time?
If Not Page.IsPostBack Then
FillDataGrid()
End If
End Sub

Sub FillDataGrid()

' Create a new dataset to contain categories' records
dsNorthwind = New DataSet()

' Fill the dataset retrieving data from the database
daNorthwind.Fill(dsNorthwind)

' Set the DataSource property of the DataGrid
dgNorthwind.DataSource = dsNorthwind.Tables(0).DefaultView

' Bind the dataset data to the DataGrid
dgNorthwind.DataBind()
End Sub

Sub btnInsert_Click(Sender As Object, E As EventArgs)
' If user has filled every text box correctly...
If Page.IsValid Then

' Create a temporary dataset to contain the new record
Dim dsTemp As New DataSet()

' Fill the temporary dataset
daNorthwind.Fill(dsTemp)

' Create a new row
Dim r As DataRow = dsTemp.Tables(0).NewRow()

' Add the category name, reading its value from the text box
r("CategoryName") = txtCategory.Text

' Add the category description, reading its value from the text box
r("Description") = txtDescription.Text

' Add the new row into the dataset's rows collection
dsTemp.Tables(0).Rows.Add(r)

' Update the database using the temporary dataset
daNorthwind.Update(dsTemp)

' Usually, you have to call the AcceptChanges() method in order to align the
' dataset with records in the database. Because this is a temporary dataset,
' we can omit this instruction.
' dsTemp.AcceptChanges()

' Refresh the data grid to display the new record
FillDataGrid()
End If
End Sub
 
Hi, I saw this post and wondered if you could help me. I'm trying to add sql data to a datagrid from an adodb.connection not sqlconnection. Any ideas ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top