Search code examples
sqlvb.netdatagridvisual-studio-2003

Populating datagrid1.view with a SQL Server stored procedure


I got a stored procedure in SQL Server I created some inner joins and now how will I populate my datagrid using that stored procedure.

Here is my code that is not working

Dim cmd As New SqlCommand
        Dim reader As SqlDataReader

        cmd.CommandText = "OfficeEquipmentProfile"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = sqlconn

        sqlconn.Open()



        sAdapter = New SqlDataAdapter(cmd)
        sBuilder = New SqlCommandBuilder(sAdapter)
        sDs = New DataSet
        sAdapter.Fill(sDs, "tblOfficeEquipmentProfile")
        sAdapter.Fill(sDs, "tblDepartment")
        sAdapter.Fill(sDs, "tblLocation")
        sAdapter.Fill(sDs, "tblOfficeEquipmentCategory")
        sAdapter.Fill(sDs, "tblApplication")
        sAdapter.Fill(sDs, "tblApplicationLicense")
        sAdapter.Fill(sDs, "tblEquipmentApplication")
        sAdapter.Fill(sDs, "tblOfficeEquipmentBrand")
        sAdapter.Fill(sDs, "tblOfficeEquipmentModel")
        sAdapter.Fill(sDs, "tblOfficeEquipmentServiceOrder")
        sAdapter.Fill(sDs, "tblOfficeEquipmentPMplan")


        sTable = sDs.Tables("tblOfficeEquipmentProfile")
        sTable = sDs.Tables("tblDepartment")
        sTable = sDs.Tables("tblLocation")
        sTable = sDs.Tables("tblOfficeEquipmentCategory")
        sTable = sDs.Tables("tblApplication")
        sTable = sDs.Tables("tblApplicationLicense")
        sTable = sDs.Tables("tblEquipmentApplication")
        sTable = sDs.Tables("tblOfficeEquipmentBrand")
        sTable = sDs.Tables("tblOfficeEquipmentServiceOrder")
        sTable = sDs.Tables("tblOfficeEquipmentPMplan")

        DataGrid1.DataSource = sDs.Tables("tblOfficeEquipmentProfile, tblDepartment, tblLocation, tblOfficeEquipmentCategory, tblApplication,tblApplicationLicense, tblEquipmentApplication, tblOfficeEquipmentBrand, tblOfficeEquipmentServiceOrder,tblEquipmentPMplan")
        DataGrid1.ReadOnly = True
        'Button1.Enabled = False
        'DataGrid1.SelectionMode = DataGridViewSelectionMode.FullRowSelect


        reader = cmd.ExecuteReader()
        sqlconn.Close()

I just want to display records from the database


Solution

  • As you are returning columns from different tables and not multiple tables then you just need this code.

     Dim Command As SqlCommand = New SqlCommand()
     Command.Connection = Connection
     Command.CommandText = "OfficeEquipmentProfile"
     Command.CommandType = CommandType.StoredProcedure
    
     Dim sAdapter As SqlDataAdapter = New SqlDataAdapter(Command)
    
     Dim DataSet As DataSet = New DataSet(Command.CommandText)
    
     sAdapter.Fill(DataSet)
     DataGrid1.DataSource = DataSet.Tables(0)