Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have datagridview ,combobox and bowse button in my project .I will choose Excel file on click bowse button .i was bind to combobbox from datagridview.I want to show datagridview from selected value of combobox. Error :"No value given for one or more required parameters " at adapter.Fill(dt) .In my code

VB
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click

        DataGridView1.Columns("Column1").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
        Try


            Dim ofd As New OpenFileDialog
            ofd.Title = "Please select the excel which you want to import"
            ofd.Filter = "Excel files (*.xls)|*.xlsx"
            If ofd.ShowDialog = DialogResult.OK Then
                DataGridView1.Visible = True

                filename = ofd.FileName
                Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & filename & ";Extended Properties=""Excel 12.0;HDR=YES"";"
                Dim con As OleDbConnection = New OleDbConnection(strin)
                If con.State = ConnectionState.Closed Then
                    con.Open()
                    Dim command As OleDbCommand = New OleDbCommand()
                    command.CommandText = "Select * from [Sheet1$]  "
                    command.Connection = con
                    Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
                    adapter.SelectCommand = command
                    Dim dt As New DataSet
                    adapter.Fill(dt, "DT")
                    DataGridView1.DataSource = dt.Tables(0)
                    con.Close()
                    Bind(filename)
                End If
            Else

                Exit Sub
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

 Public Sub Bind(ByVal strExcelFilePath As String)

        Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strExcelFilePath & ";Extended Properties=""Excel 12.0;HDR=YES"";"
        Dim con As OleDbConnection = New OleDbConnection(strin)
        If con.State = ConnectionState.Closed Then
            con.Open()
            Dim command As OleDbCommand = New OleDbCommand()
            command.CommandText = "Select Distinct Department from [Sheet1$] "
            command.Connection = con
            Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
            adapter.SelectCommand = command
            Dim dt As New DataSet
            adapter.Fill(dt, "DT")
            ComboBox1.DisplayMember = dt.Tables(0).Columns(0).ColumnName
            ComboBox1.DataSource = dt.Tables(0)
            con.Close()
        End If


    End Sub
 Public Sub ComboBind(ByVal strExcelFilePath As String)

        Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strExcelFilePath & ";Extended Properties=""Excel 12.0;HDR=YES"";"
        Dim con As OleDbConnection = New OleDbConnection(strin)
        If con.State = ConnectionState.Closed Then
            con.Open()
            Dim command As OleDbCommand = New OleDbCommand()
            command.CommandText = "Select Distinct Department from [Sheet1$] where  Department=" & ComboBox1.Text
            command.Connection = con
            Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
            adapter.SelectCommand = command
            Dim dt As New DataSet
            'adapter.Fill(dt, "DT")
            adapter.Fill(dt)

            dt.Tables(0).Columns(0).ColumnName = ComboBox1.DisplayMember
            DataGridView1.DataSource = dt.Tables(0)

        End If


    End Sub
 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        '  Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & filename & ";Extended Properties=""Excel 12.0;HDR=YES"";"


        If ComboBox1.SelectedIndex = 0 Then

            ComboBind(filename)

        End If

    End Sub
Posted
Comments
sahabiswarup 4-Dec-13 0:07am    
pasting the code, is not enough to describe your question, kindly check your question before posting here, without understand the problem how it is possible to answer.

1 solution

When you bind the data in gridview at that time keep a lable in the edit item templte same as the dropdown
bind the id feild with the lable and keep visiblity false

when you edit the row at that time in databound method populate the dropdwon and set seletedindex value with the lable id value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900