Solved

gridview select based on a sp

Posted on 2011-02-17
6
369 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
  • 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 500 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regular expression help 2 22
Example works but why do I need to click twice on button 4 36
C# Gridview 1 27
Calculate number of nights between two dates 5 17
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now