I have 1 Table Called "Menu" there are some textboxes and a combobox.
I want to fill data in the Textbox called "Menu_Rate" on Selection of combobox selected item & coressponding rate of the Menu shoud be displayed in the textbox.

This is my Code

con.ConnectionString = "Data Source=localhost;Integrated Security=true;Initial Catalog=Restaurant"
        cmd.CommandText = "select Menu_Rate from Menu where Menu_Name='" & cbMenu.SelectedItem & "'"
        cmd.Connection = con
        da.SelectCommand = cmd
        con.Open()
        da.Fill(ds, "Menu")
        con.Close()
        con.Dispose()
        cmd.Dispose()

        TxtRate.Text = ds
    End Sub

You will want to create a data table form the database.


Then you will use the selected index of the combobox to pull from the datatable.

Example...

Select Case cbMenu.SelectedItem

Case 1
TxtRate.Text = dt.Rows(0).Column(1)
......


This will get the value stored in the second column of the corrosponding table row.

Assuming the value is in the second column of the same table.

thanxx

But it showing error as......

Value of type 'System.Data.DataRow' cannot be converted to 'System.Windows.Forms.TextBox'.

I am sorry......

I meant.....

TxtRate.Text = dt.Rows(0).Item(1)

But it is showing the same error...

i changed My code to this......

Private Sub cbMenu_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbMenu.SelectedIndexChanged
        Dim tem As String
       [U] tem = cbMenu.SelectedItem[/U]
        con.ConnectionString = "Data Source=localhost;Integrated Security=true;Initial Catalog=Restaurant"
        cmd.CommandText = "select Menu_Rate from Menu where Menu_Name='" & tem & "'"
        cmd.Connection = con
        da.SelectCommand = cmd
        con.Open()
        da.Fill(ds, "Menu")
        con.Close()
        con.Dispose()
        cmd.Dispose()
        Dim row As DataRow

        For Each row In ds.Tables("Menu").Rows
            TxtRate.Text &= row("Menu_Rate") & " "
        Next
    End Sub

Now The error is in the underlined part..

it shows...
Conversion from type 'DataRowView' to type 'String' is not valid.

sorry i give the code for integers, not string. :P

don't know how to delete post

Have you tried the .ToString() function?

Example...

TxtRate.Text = dt.Rows(0).Item(1).ToString

Have you tried the .ToString() function?

Example...

TxtRate.Text = dt.Rows(0).Item(1).ToString

Try this...

Dim conn As New SqlConnection(ConnectionString)
        Dim abc As String
        abc= cbMenu.Text 'the value selected in combo box       
        Dim strSQL As String = "select Menu_Rate from Menu where Menu_Name='" & abc & "'"
        Dim da As New SqlDataAdapter(strSQL, conn)
        Dim ds As New DataSet
        da.Fill(ds, "Menu")
        With txtRate ' your text box name
            .DataSource = ds.Tables("Menu") 'your table name            
            .DisplayMember = "Menu_Rate"
          '  .ValueMember = "ProductID"
        End With

Tried......

But it shows

'DataSource' is not a member of 'System.Windows.Forms.TextBox'

'DisplayMember' is not a member of 'System.Windows.Forms.TextBox'.

But Thanxx.....

Please Help .... I hv to submit My Project In Next week....

Tried......

But it shows

'DataSource' is not a member of 'System.Windows.Forms.TextBox'

'DisplayMember' is not a member of 'System.Windows.Forms.TextBox'.

But Thanxx.....

Please Help .... I hv to submit My Project In Next week....

Hi bhavan kamble!

I've tryed to fill a textbox which was dependent on a combobox choice.. what i did was this:

conn.ConnectionString = ("Data Source=Laptop-VAIO\SQLEXPRESS;Initial Catalog=AMR;Integrated Security=True")
txtState.Clear()
        txtState.Enabled = True

        '-------------------------------Fill txtState-----------------------------

        Dim StateTable As New DataTable
        StateTable.Clear()
        Dim sqlCmd As New SqlCommand("select DISTINCT State_LCL from T_MUNICIPALITY,T_MUNICIPALITY_STREETS1 where  T_MUNICIPALITY.Municipality_LCL=T_MUNICIPALITY_STREETS1.Municipality_LCL and T_MUNICIPALITY_STREETS1.Municipality_LCL='" & cboMunicipality.Text & "'", conn)
        conn.Close()
        Dim result As String
        Try
            conn.Open()
            result = sqlCmd.ExecuteScalar()

            If result IsNot Nothing Then
                txtState.Text = result.ToString()
                'MessageBox.Show(txtState.Text)
            End If
        Catch ex As SqlException
            MessageBox.Show(ex.Message)
        Finally
        End Try

I wish I helped!

Thanxxx....... yaarrrr, thank you so much....

Its done......
Thanxx....for your valuable......time.

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.