locked
ASP.NET VB code RRS feed

  • Question

  • User-1699810686 posted

    I need some help, just for our school 

    I use dropdownlist in my form with database to get the value of each list and i want to automatically input on txtamount.Text but it doesn't fix

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Try
    txtpackage.Items.Clear()

    'Open connection
    MySQLConn.ConnectionString = ServerString
    MySQLConn.Open()
    Dim sql As String = "SELECT pname, price1 FROM package"
    Dim a = New MySqlCommand(sql, MySQLConn)
    Dim ds As New DataSet
    Dim da As New MySqlDataAdapter(a)
    da.Fill(ds)
    Using sdr As MySqlDataReader = a.ExecuteReader()
    While sdr.Read()
    Dim item As New ListItem()
    item.Text = sdr("pname").ToString()
    item.Value = sdr("price1").ToString()
    txtpackage.Items.Add(item)
    End While
    End Using
    da.Dispose()
    a.Dispose()
    MySQLConn.Close() '<-- close connection
    Catch ex As Exception '<-- catch any system error and display in msg box
    txtamount.Text = "SYSTEM ERROR!" + ex.ToString

    End Try
    End Sub
    Protected Sub txtpackage_SelectedIndexChanged(sender As Object, e As EventArgs) Handles txtpackage.SelectedIndexChanged
    txtamount.Text = txtpackage.SelectedValue.ToString 'to get price of value
    End Sub

    Wednesday, February 6, 2019 2:11 AM

All replies

  • User1724605321 posted

    Hi gette,

    To bind dropdownlist in vb.net , you can refer to below code sample  :

    <asp:DropDownList ID = "ddlCustomers" runat="server">
    </asp:DropDownList>

    Code behind :

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
            Using con As New MySqlConnection(constr)
                Using cmd As New MySqlCommand("SELECT CustomerId, Name FROM Customers")
                    cmd.CommandType = CommandType.Text
                    cmd.Connection = con
                    con.Open()
                    ddlCustomers.DataSource = cmd.ExecuteReader()
                    ddlCustomers.DataTextField = "Name"
                    ddlCustomers.DataValueField = "CustomerId"
                    ddlCustomers.DataBind()
                    con.Close()
                End Using
            End Using
            ddlCustomers.Items.Insert(0, New ListItem("--Select Customer--", "0"))
        End If
    End Sub

    Then you could use the SelectedIndexChanged event of ASP.Net DropDownList :

    https://www.aspsnippets.com/Articles/ASPNet-DropDownList-SelectedIndexChanged-event-example-in-C-and-VBNet.aspx 

    Best Regards,

    Nan Yu

    Wednesday, February 6, 2019 5:25 AM
  • User-1699810686 posted

    thank you 

    Thursday, February 7, 2019 2:37 AM