Click here to Skip to main content
16,020,347 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedValue.ToString() <> " " Then
            Dim Country_id As Integer = Convert.ToString(ComboBox1.SelectedValue.ToString())
            fillstate(Country_id)
            ComboBox3.SelectedIndex = 0

        End If



    End Sub
    Private Sub fillstate(ByVal Country_id As Integer)
        Dim adpt As New SqlDataAdapter
        Dim ds As New DataSet
        Dim con As New SqlConnection
        con.ConnectionString = str
        cmd = New SqlCommand("SELECT S_id,S_name from state where C_id=@C_id", con)
        cmd.Parameters.AddWithValue("@C_id", Country_id)
        adpt.SelectCommand = cmd
        adpt.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            ComboBox2.ValueMember = "S_id"
            ComboBox2.DisplayMember = "S_name"
            ComboBox2.DataSource = ds.Tables(0)

        End If


the above code is used arguments as integer ie,country_id as integer


if i use country_id as string where should i correct to make it pass as arguments
Posted
Comments
Richard MacCutchan 5-Jan-14 7:34am    
Just change it from integer to string. Although it appears it is an integer value in your database so you will need to change that also. What exactly are you trying to achieve?

1 solution

If I was you, I'd look at going the other way:
VB
Dim Country_id As Integer = Convert.ToString(ComboBox1.SelectedValue.ToString())
fillstate(Country_id)
Is a little...um...odd.
VB
ComboBox1.SelectedValue
May be an integer already, particularly if you have read it directly form the DB. So...
VB
ComboBox1.SelectedValue.ToString()
Makes it a string. So...
VB
Convert.ToString(ComboBox1.SelectedValue.ToString())
Converts the string to...a string...
VB
Dim Country_id As Integer = Convert.ToString(ComboBox1.SelectedValue.ToString())
Then uses an implicit conversion to change it to an integer to pass to your method, which takes an integer...
So, why not just pass the Value as an integer in the first place, or call CInt on it to ensure it is an integer?

Playing "musical chairs" with variable types is just a good way to make your code more confusing and less reliable...
 
Share this answer
 
Comments
shakeer mp 5-Jan-14 8:35am    
i have passed as County_id as integer now i want to pass this as string....

when and where i should change to p
shakeer mp 5-Jan-14 8:41am    
pass as an arguments o change as strings
OriginalGriff 5-Jan-14 9:28am    
Why pass it as a string if you want a number anyway?

But... just change two lines:
Dim Country_id As String = ComboBox1.SelectedValue.ToString()
and
Private Sub fillstate(ByVal Country_id As String)
Done!
shakeer mp 5-Jan-14 12:06pm    
i am new to vb i am little confused i have used c# web application but now i am in vb windows applications
thanks a lot
OriginalGriff 5-Jan-14 12:17pm    
If you aren't sure what it should be in VB, try writing it in VB and C#.
Then feed the VB through here:
http://www.developerfusion.com/tools/convert/vb-to-csharp/
and the C# through here:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
and see what the difference is!
It's not perfect (it compiles it to IL and then decompiles that back to the other language so it doesn't necessarily get the "best way") but it might give you a few pointers.

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