Hi

first of all am new to progarmming. Am using using combo box control in vb.net

combo box getting filled properly

Public Sub new_entry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim cn As New SqlClient.SqlConnection("Data Source=KAU-4A76A124FB9;Initial Catalog=SRRY;Integrated Security=True")
    cn.Open()
    Dim adp As New SqlDataAdapter("select * from Bank_Details order by (bank_id)", cn)
    Dim ds As New DataSet
    adp.Fill(ds)
    With Combo_Bank_1
        .DataSource = ds.Tables(0)
        .DisplayMember = "Bank_Name"
        .ValueMember = "Bank_Id"
        .SelectedIndex = 0
    End With
    cn.Close()
End Sub

But now i want on selected value of Combo_Bank_1 another combo box should get filled

i have tried this

Private Sub Combo_Bank_1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Combo_Bank_1.SelectedIndexChanged
    Dim cn As New SqlClient.SqlConnection("Data Source=KAU-4A76A124FB9;Initial Catalog=SRRY;Integrated Security=True")

    Dim adp1 As New SqlDataAdapter("select Bank_Name from Bank_Details where bank_id='" & Combo_Bank_1.SelectedValue & "'", cn)

    Dim ds1 As New DataSet

    adp1.Fill(ds1)

    txt_PName.Text = ds1.Tables(0).Rows(0).Item(0)

    If Combo_Bank_1.SelectedValue = 0 Then
        MsgBox("select")
    Else
        txt_PName.Text = ds1.Tables(0).Rows(0).Item(0)
    End If

    cn.Close()
End Sub

am getting error

Operator '&' is not defined for string "select Bank_Name from Bank_Detai" and type 'DataRowView'.

i have tried converting it to string , also to int
am able to display the value of selectd index, selected value but not able to passed in sql query

if i entered value directly i.e

Dim adp1 As New SqlDataAdapter("select Bank_Name from Bank_Details where bank_id=1", cn)

Its working. but not able to passed the value from Combo_Bank_1

Plz help

thanks

Kaustubh

Dani AI

Generated

Good catch by on avoiding string concat, but the root cause of the error saw is that during data binding SelectedValue can briefly be a DataRowView, and SelectedIndex is just the zero-based list position (which is why you observed the off-by-one). Use the committed-selection event, guard the value, and query with parameters (never string-concat SQL). Also prefer ExecuteScalar for a single value to avoid the "There is no row at position 0" crash.

Example: fill a textbox from the selected bank id.

Private Sub Combo_Bank_1_SelectionChangeCommitted(sender As Object, e As EventArgs) _
    Handles Combo_Bank_1.SelectionChangeCommitted

    If Combo_Bank_1.SelectedValue Is Nothing OrElse Not IsNumeric(Combo_Bank_1.SelectedValue) Then Exit Sub
    Dim bankId As Integer = CInt(Combo_Bank_1.SelectedValue)

    Using cn As New SqlConnection(My.Settings.SRRY_ConnString),
          cmd As New SqlCommand("SELECT Bank_Name FROM Bank_Details WHERE Bank_Id = @BankId", cn)
        cmd.Parameters.Add("@BankId", SqlDbType.Int).Value = bankId
        cn.Open()
        Dim nameObj = cmd.ExecuteScalar()
        txt_PName.Text = If(nameObj IsNot Nothing, CStr(nameObj), String.Empty)
    End Using
End Sub

Example: cascade a second combo (branches) based on the bank selection.

Private Sub LoadBranches(bankId As Integer)
    Using cn As New SqlConnection(My.Settings.SRRY_ConnString),
          cmd As New SqlCommand("SELECT BranchId, BranchName FROM Bank_Branches WHERE Bank_Id=@BankId ORDER BY BranchName", cn),
          da As New SqlDataAdapter(cmd)
        cmd.Parameters.Add("@BankId", SqlDbType.Int).Value = bankId
        Dim dt As New DataTable()
        da.Fill(dt)
        Combo_Branch_2.DisplayMember = "BranchName"
        Combo_Branch_2.ValueMember = "BranchId"
        Combo_Branch_2.DataSource = dt
    End Using
End Sub

Notes:

  • Do not quote numeric IDs in SQL and avoid concatenation entirely.
  • If you must use SelectedIndexChanged, add a guard like If TypeOf Combo_Bank_1.SelectedValue Is Integer Then ....
  • Always check for empty results before reading rows to prevent index errors.
  • In ASP.NET, use SelectedIndexChanged with AutoPostBack="true" and the same parameterized pattern.

Recommended Answers

All 10 Replies

Hi,
First of all in line 30 I think you should change to '" & Combo_Bank_1.SelectedIndex.ToString & "'
Try this and tell us..

Hi,
First of all in line 30 I think you should change to '" & Combo_Bank_1.SelectedIndex.ToString & "'
Try this and tell us..

HI

Now am getting error

There is no row at position 0.

Ok, look what I have done.. I had also cascading combobox..

Dim PostcodeDA As New SqlDataAdapter()
        Dim DSpostcode As New AMRtoSQLDataSet
        PostcodeDA.SelectCommand = New SqlCommand()
        PostcodeDA.SelectCommand.Connection = conn
        PostcodeDA.SelectCommand.CommandText = "SELECT Postcode,StreetName_LCL FROM T_MUNICIPALITY_STREETS WHERE StreetName_LCL= '" & cboStreetName1.SelectedIndex.ToString & "'"
        PostcodeDA.Fill(DSpostcode, "PostcodeTable")
        cboPostcode.DataSource = DSpostcode.Tables("PostcodeTable")
        cboPostcode.DisplayMember = "Postcode"
        cboPostcode.ValueMember = "Postcode"
        cboPostcode.SelectedValue = 0

The cboPostcode is depends on cboStreetName choice...

Hi,
First of all in line 30 I think you should change to '" & Combo_Bank_1.SelectedIndex.ToString & "'
Try this and tell us..

Hi

with

Combo_Bank_1.SelectedIndex.ToString

its giving me the 1 value before selected value

while i want serach

Combo_Bank_1.SelectedItem.ToString

Its giving me the error

Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type int.

Skaran

thanks for u r reply

its working with selected index property but its was giving one earlier value so i have added

--select-- value in table at 0 position

now the code is working fine.

Thanks

kaustubh

commented: please give full source code.... +0

I'm glad I could help! :)

And if it's easy for you please mark the problem as solved. Thanks!

how to marked as solve???

Down the page has a question "Has this thread been answered?". You have to choose
" Mark this Thread as Solved "

plz give solved source code .............

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.