Suppose der is one database name Customer which have columns like Cust_ID,Cust_Name,Cust_Mob,Cust_Address

And on Form der are one list box which contains all Cust_Id from database
and 4 textbox
so when user click on the listbox having id as 1 it should display name, mob and address of cust_id 1 in 4 textbox respectively pls help me asap........

Thanks in advance....

Recommended Answers

All 18 Replies

Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
dbSource = "Data Source=XX-B4C521B850\SQLEXPRESS;Initial Catalog=TT;Integrated Security=True"

con.ConnectionString = dbSource
con.Open()
sql = ""
sql = "Select Cust_Fn from Cust where Cust_Id='" & ListBox1.SelectedIndex & "' "
da = New SqlClient.SqlDataAdapter(sql, con)
da.Fill(ds, "Cust")
con.Close()

TextBox1.Text = ds.Tables("Cust").Rows(0).Item(0)
End Sub

Pls help the problem is dat when i run d form and if i select Cust_ID=1
it shows me name of Cutomer having ID 1
but if i click 2 it does not show the name of Cust_Id=2 but it shows Name of Cust_Id=1 only pls help me out

Member Avatar for stbuchok

Try using OnSelectedIndexChanged event instead of OnClick. Why is your ID column a string? Also what does Select Cust_Fn from Cust where Cust_Id='2' return from the database?

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        dbSource = "Data Source=xx-B4C521B850\SQLEXPRESS;Initial Catalog=TT;Integrated Security=True"

        con.ConnectionString = dbSource
        con.Open()
        sql = "Select Cust_Fn from Cust where Cust_Id='" & ListBox1.Text & "'   "
        da = New SqlClient.SqlDataAdapter(sql, con)
        da.Fill(ds, "Cust")
        con.Close()

        TextBox1.Text = ds.Tables("Cust").Rows(0).Item(0)
    End Sub

It does not work
the line

sql = "Select Cust_Fn from Cust where Cust_Id='" & ListBox1.Text & "'  "

says dat select Cust_Fn(Customer FIRSTName) from Cust(CUSTOMER) WHERE Cust_Id(Customer_ID)=listbox1.text

Member Avatar for stbuchok

For the dropdown list, did you set the AutoPostBack property to true? When you run Select Cust_Fn from Cust where Cust_Id='2' against the database WHAT DOES IT RETURN? Don't try to give me an explanation of what you have, I can read the query, I know what it should do, but is it actually doing it is the question.

commented: I Like It. +8

first of all sorry dont take me wrongly!!!!!

I am using listbox and not combobox and its windows application and not web application for which i have to set autopostback propery=true

Following is my database structure
Cust_ID(Column1)
1
2
3
4
Cust_Fn(Column2)
xyz
abc
sdd
ddf

when i select 1 from listbox it shows me xyz in textbox but when i select 2 insted of abc it shows me xyz.....


Thanks ur help really appreciated....

You should do this .

1.
when you fill listbox you should set list box value on Cust_ID

like: ListBox1.ValueMember = "CusT_ID"

2.

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

query = "select Cust_ID,Cust_Name,Cust_Mob,Cust_Address from table where Cust_ID ='" & ListBox1.SelectedValue & "'"

Dim fill As New DataSet
fill = ObjSQLQuery.PopDataSet(query)
If fill.Tables(0).Rows.Count >= 1 Then
textbox1.text=fill.Tables(0).Rows(0)("Cust_ID").ToString
textbox2.text=fill.Tables(0).Rows(0)("Cust_Name").ToString
textbox3.text=fill.Tables(0).Rows(0)("Cust_Mob").ToString
textbox4.text=fill.Tables(0).Rows(0)("Cust_Address").ToString
Else
Msgbox("No Record Found")
End If
End Sub

Hope It helps you...

PopDataSet is a memeber of whom
How to declare ObjSQLQuery


I have used popdataset in developing windows application with c#, had never used it in windows application with vb
How to use it in windows application with vb....
Pls help!!!!

So you must post this in C# forum dear this is vb.net forum.

I've never liked using datasets and stuff. Not comfortable with automated things. maybe I am backward in code.. You could try direct SQL results and manually set the fields

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    dbSource = "Data Source=xx-B4C521B850\SQLEXPRESS;Initial Catalog=TT;Integrated Security=True"
     Dim db_command as new OleDBcommand, reader as Oledbreader 'or whatever method you are using.
    con.ConnectionString = dbSource
    con.Open()
    db_command.commandtext = "Select Cust_Fn from Cust where Cust_Id=" & ListBox1.SelectedIndex 'Dropped the single quotes
    db_command.connection=con
     reader=db_command.executereader()
    if (reader.read()) 
    TextBox1.Text = reader("cust_fn")
    reader.close()
    con.close()
    End if
    End Sub

No ur taking mw wrongly its vb.net doubt only but how to declare "ObjSQLQuery"
as we
for eg :
Dim ObjSQLQuery As ?

Don't know what you are talking about. I use the OleDbCommand(SYstem.Data.OledB.OledbCommand) or whatever method you use for connecting. System.Data.OleDb.Oledbreader for SQL results reader.

@saj
No ur taking mw wrongly its vb.net doubt only but how to declare "ObjSQLQuery"
as we
for eg :
Dim ObjSQLQuery As ?

ObjSQLQuery is the class where PopDataSet is the function which returns ds this the PopDataSet function :


Public Function PopDataSet(ByVal SQLStr As String) As DataSet
Dim ds As New DataSet

If ConnectionClass.SQLCon.State = ConnectionState.Closed Then
ConnectionClass.SQLCon.Open()
End If
adp = New SqlDataAdapter(SQLStr, ConnectionClass.SQLCon)

adp.Fill(ds)
Return ds
End Function

hope you get it...

Dis is what i wanted d simplest way....
But thanks 4 ur help...

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Connect As String = "Data Source=XXXX-578AC20261\SQLEXPRESS;Initial Catalog=TT;Integrated Security=True"
        Dim con As New SqlConnection(Connect)

        Dim SQL As String = "SELECT * FROM Cust"
        Dim cmd As New SqlCommand(SQL, con)

        Dim adapter As New SqlDataAdapter(cmd)
        Dim dsCust As New DataSet()
        con.Open()
        adapter.Fill(dsCust, "Cust")
        con.Close()
        'gridDB.DataSource = dsCust.Tables("Cust")
        txtFirstName.DataBindings.Add("Text", dsCust.Tables("Cust"), "Cust_Fn")
        txtLastName.DataBindings.Add("Text", dsCust.Tables("Cust"), "Cust_Ln")
        lstID.DataSource = dsCust.Tables("Cust")
        lstID.DisplayMember = "Cust_ID"
        'dtBirth.DataBindings.Add("Value", dsCust.Tables("Cust"), "BirthDate")
    End Sub
End Class

This is not the simplex way dear thats the lengthy way. you cant write whole code again if you want any data in the other form easy way to call a function again and again when you want but if you are ok with it no problem best of luck dear!

I am getting what you are saying but currently this is the most simple currently now because of only one form....
But the code given by you is the proper code ....

its ok dear ALL the best ov luck to you.

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.