CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: SQL Server?

  1. #1
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150

    Lightbulb SQL Server?

    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

  2. #2
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236

    Re: SQL Server?


  3. #3
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150
    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

  4. #4
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    You mean something like that:

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

  5. #5
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150
    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"))

  6. #6
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    You can pass a DataRow object into a Function:

    Code:
        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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured