Click to See Complete Forum and Search --> : SQL Server?


nolc
May 28th, 2002, 01:34 PM
Hello,
Would anyone have an idea how to programmatically :
1. Connect to an SQL Server database
2. Select data by using either a SQL Stored Procedure or View
3. Then bind it to a DataGrid?


Public SQLconn As SqlConnection
Public strConn As String = "SERVER=SER01;DATABASE=dbNoelTest;UID=sa;PWD=;"
Public strSQL As String

SQLconn = New SqlConnection(strConn)
strSQL = "SELECT * FROM tblOrderSearch"
Dim SQLda As New SqlDataAdapter(strSQL, strConn)
Dim SQLds As New DataSet()



.....thanks

Akim
May 29th, 2002, 07:06 AM
You'll find the answer here:
http://msdn.microsoft.com/howto/visualbasic.asp

nolc
May 29th, 2002, 07:56 AM
Thanks Akim,
I figured it out last night.


Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim cmdString As String = "spSELECTUnassignedOrders" '"SELECT * FROM
Employees"
Dim connString As String =
"uid=sa;pwd=;database=northwind;server=server"
Dim myConnection As SqlConnection = New SqlConnection(connString)

Try
myConnection.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter(cmdString,
myConnection)
Dim ds As DataSet = New DataSet()
da.Fill(ds, "Employees")
Me.DataGrid1.DataSource = ds.Tables("Employees")
'.DefaultViewManager

'Dim dt As New DataTable()
'dt = ds.Tables(0)
'dt.TableName = "Employees"
'Dim dv As DataView = New DataView(dt)
'dv.AllowNew = False
'dv.AllowEdit = False
'DataGrid1.DataSource = dv


Catch ae As SqlException
MessageBox.Show(ae.Message)

End Try

End Sub


Akim,
......you wouldn't happen to know how to grab the data once you double click a row?


thanks again

Akim
May 29th, 2002, 08:13 AM
You mean something like that:

MessageBox.Show(DataSet.Tables("TableName").Rows(DataGrid1.CurrentRowIndex).Item("FieldName"))

nolc
May 29th, 2002, 10:10 AM
I think so.....but if I throw this into a seperate Sub, will I have to create a new DataSet, pass it or can I use the current one? Also, will this display the whole row of data or just the current cell?


MessageBox.Show(DataSet.Tables("TableName").Rows(DataGrid1.CurrentRowIndex).Item("FieldName"))

Akim
May 29th, 2002, 10:47 AM
You can pass a DataRow object into a Function:

Function DataProc(ByVal rowDS As DataRow)
MessageBox.Show(rowDS("FieldName"))
End Function

Private Sub DataGrid1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.DoubleClick
DataProc(ds.Tables("TableName").Rows(DataGrid1.CurrentRowIndex))
End Sub
Depends on what you want to pass.