Solved

gridview select based on a sp

Posted on 2011-02-17
6
367 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

21 Experts available now in Live!

Get 1:1 Help Now