?
Solved

gridview select based on a sp

Posted on 2011-02-17
6
Medium Priority
?
374 Views
Last Modified: 2012-05-11
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)

            FileUpload1.SaveAs(FilePath)
            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  
        connExcel.Open()

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

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

        'Read Data from First Sheet 
        connExcel.Open()
        cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
        oda.SelectCommand = cmdExcel
        oda.Fill(dt)
        connExcel.Close()

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

    End Sub

Open in new window

0
Comment
Question by:aneilg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 3

Expert Comment

by:jmro20
ID: 34920703
What column or columns on the gridview will have the value(s) that the sp will be receiving as parameter(s)?
0
 
LVL 3

Expert Comment

by:jmro20
ID: 34921012
Try this:
0
 
LVL 3

Accepted Solution

by:
jmro20 earned 2000 total points
ID: 34921088
   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
        Else
            'message - must select customer
        End If

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

        yourCommand.Parameters.Add(param)

        Dim ds As New DataSet

        MySqlDataAdapter = New SqlDataAdapter(yourCommand)

        MySqlDataAdapter.Fill(ds, "Records")

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

        cnn.Close()

    End Sub
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:aneilg
ID: 34924401
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.
0
 
LVL 3

Expert Comment

by:jmro20
ID: 34925297
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?
0
 

Author Closing Comment

by:aneilg
ID: 35163795
thanks.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question