I’m trying to create an option to select a specific entry in a SQL table if multiples exists. The Sub listed below is for a textbox (MOTxt) and a button (GotMO) for checking an MO# to pull from SQL. Based on the number entered, will populate other text boxes on the page with that row’s data. There are exceptions to this, where the MONumber will be entered more than once with different data associated with each entry. So, here’s my situation. If we have a duplicate MONumber listed in the table, it will populate the textboxes with the first entry it finds for an update.
I would like to create a dynamic dropdownlist (that displays only if multiples are found) displaying the ‘Name’ field entries for each MONumber found, then once the correct name is selected from that list, to then populate the textboxes with that selected datarow.
Here’s my code behind:
Protected Sub GotMONum_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GotMO.Click
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim MOLength As String = MOTxt.Text
Dim TieOffTemp As Integer
Dim QTYTemp As Integer
Dim MySelectQuery As String = "SELECT Name,ProdLine,NoRods,TieOffs FROM z_md_Outwrap WHERE MONumber = @MONum"
Dim myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("GLoomisDBConnectionString").ConnectionString)
Dim myCommand As New SqlCommand(MySelectQuery, myConnection)
Message2.InnerHtml = ""
Message.InnerHtml = ""
If (Me.MOTxt.Text = "") Then
MsgBox("Please enter an MO Number to check")
Else
' Fills fields based on MO Number
myCommand.Parameters.AddWithValue("@MONum", MOTxt.Text)
myConnection.Open()
da.SelectCommand = myCommand
da.Fill(ds, "tblData")
myConnection.Close()
If ds IsNot Nothing AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
NameTxt.Text = ds.Tables("tblData").Rows(0).Item("Name")
ProdLineTxt.Text = ds.Tables("tblData").Rows(0).Item("ProdLine")
NoRodsTxt.Text = ds.Tables("tblData").Rows(0).Item("NoRods")
TieOffsTxt.Text = ds.Tables("tblData").Rows(0).Item("TieOffs")
If Integer.TryParse(NoRodsTxt.Text, QTYTemp) Then
If Integer.TryParse(TieOffsTxt.Text, TieOffTemp) And TieOffTemp > 0.0 Then
AmountVal.Text = "$" & (QTYTemp * TieOffTemp * 0.18)
End If
End If
Else
Message.InnerHtml = "No MO# found as entered. Please check for errors."
Message.Style("color") = "red"
End If
End If
End Sub
If I were doing this, I would change the code so that the contents in your click function are in a separate functions so that you can recall it easily from other functions. I would use a strongly typed object instead of a dataset for added symplicity. Also add a couple of string arguments to the “Load Data” function to accept monum and name strings.
To find multiples, just check the count of what the database returns (datatable.rows.count or StronglyTypedObject.count)
Next, create a invisible dropdownlist control and set it to visible if your row count is > 0.
using Linq, binding the dropdownlist is fairly straight forward. Then call the new “load data” event on the onselectedindexchanged event passing the name selected along with the monumber.
I will not go into filling a strongly typed object as this question is already asking for alot, but to get you started… The following code example is just that and should not be taken as the actual code you will use.
Hopefully this gets you on your way… – Mutek