[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

gridview select based on a sp

i import a list of customers into a gridview from a spread sheet.
What i want to do is get the user to click on another button then populate a gridview will all the customer details.
Public Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        If FileUpload1.HasFile Then
            Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
            Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)

            Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
            Dim FilePath As String = Server.MapPath(FolderPath + FileName)

            Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
        End If

    End Sub

    Public Sub Import_To_Grid(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String)

        Dim conStr As String = ""

        Select Case Extension
            Case ".xls"
                'Excel 97-03 
                conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString()
                Exit Select
            Case ".xlsx"
                'Excel 07 
                conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString()
                Exit Select
        End Select

        conStr = String.Format(conStr, FilePath, isHDR)

        Dim connExcel As New OleDb.OleDbConnection(conStr)
        Dim cmdExcel As New OleDb.OleDbCommand()
        Dim oda As New OleDb.OleDbDataAdapter()

        Dim dt As New DataTable()

        cmdExcel.Connection = connExcel

        'Get the name of First Sheet  

        Dim dtExcelSchema As DataTable
        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, Nothing)

        Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()

        'Read Data from First Sheet 
        cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
        oda.SelectCommand = cmdExcel

        'Bind Data to GridView 
        GridView1.Caption = Path.GetFileName(FilePath)
        GridView1.DataSource = dt

    End Sub

Open in new window

  • 4
  • 2
1 Solution
What column or columns on the gridview will have the value(s) that the sp will be receiving as parameter(s)?
Try this:
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cnn As New SqlConnection("")
        Dim yourCommand As New SqlCommand("yourSP", cnn)
        Dim MySqlDataAdapter As New SqlDataAdapter

        yourCommand.CommandType = CommandType.StoredProcedure

        If cnn.State <> ConnectionState.Open Then cnn.Open()
        Dim param As SqlParameter = New SqlParameter("@parameterName", SqlDbType.Int)

        'If you are using SelectionMode = FullRowSelect
        If DataGridView1.SelectedRows.Count > 0 Then
            param.Value = DataGridView1.SelectedRows(0).Cells("ColumnNameOfTheCustomerId").Value
            'message - must select customer
        End If

        'If you are using SelectionMode = CellSelect
        If DataGridView1.SelectedCells.Count > 0 Then
            param.Value = DataGridView1.SelectedCells(0).Value
            'message - must select customer
        End If


        Dim ds As New DataSet

        MySqlDataAdapter = New SqlDataAdapter(yourCommand)

        MySqlDataAdapter.Fill(ds, "Records")

        DataGridView2.DataSource = Nothing
        DataGridView2.DataSource = ds.Tables("Records")


    End Sub
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

aneilgAuthor Commented:
thanks i'll give it a go.

an example will be,
abc company
efg company.

Theres customers will be displayed in the gridview, what i then want to do is pass each customer name into a parameter, then display the full address of the customer. either in the same gridview or a new gridview.

I need to display the full list of customers not just one.
Do you want to display the address when a user clicks on the customers gridview? or Do you want to get the address of each company after you read the excel file?
aneilgAuthor Commented:

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now