Jump to content

Load data to textboxs by using dropdown list

- - - - -

  • Please log in to reply
2 replies to this topic

#1
avosoft

avosoft

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
Hello guys,

AM trying to on page load, load all product IDs from database to dropsown list, and the on selecting anyone of the IDs, fetch data from the database, and fill textboxex on the webpage.

CAn you help me pls
Programming is all about good logic. Spend more time here

vHost for Apache:thumbup1:

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
could you post the code that you have worked until now , so that if there are corrections we can help you:)

#3
avosoft

avosoft

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
I found the solution


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    If Not IsPostBack Then

        DDLDataSource()

     End If

End Sub

Private Sub DDLDataSource()

    Dim connString As String = "Your connection string"

    Using conn As New SqlConnection(connString)

        Dim queryString As String = "SELECT * FROM Products"

        Dim comm As New SqlCommand(queryString, conn)

        Dim da As New SqlDataAdapter(comm)

        Dim dt As New DataTable()

        da.Fill(dt)

       

        DropDownList1.DataSource = dt

        DropDownList1.DataTextField = "ProductName"

        DropDownList1.DataValueField = "ProductID"

        DropDownList1.DataBind()

    End Using

End Sub

Private Sub FetchProductData()

    Dim connString As String = "Your connection string"

    Using conn As New SqlConnection(connString)

        Dim queryString As String = "SELECT * FROM Products WHERE ProductID=@ProductID"

        Dim comm As New SqlCommand(queryString, conn)

        comm.Parameters.AddWithValue("ProductID", DropDownList1.SelectedItem.Value)

        Dim da As New SqlDataAdapter(comm)

        Dim dt As New DataTable()

        da.Fill(dt)

       

         If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then

            TextBox1.Text = dt.Rows(0)("ProductID").ToString()

            TextBox2.Text = dt.Rows(0)("ProductName").ToString()

            TextBox3.Text = dt.Rows(0)("ProductCategoryName").ToString()

        End If

    End Using

End Sub

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

    If DropDownList1.SelectedIndex <> -1 Then

        FetchProductData()

    End If

End Sub

Programming is all about good logic. Spend more time here

vHost for Apache:thumbup1:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users