Answered by:
Function to return a List for a dropdownlist or combobox

Question
-
User-585544258 posted
Hi everbody. I'm new to programming, and have only done a few things. I have written a function before to delete and update records, but never anything to return values back to my call of the function. Here is my code, and I will try to explain what I'm trying to acheive.
If Not Att1 = "" Then Attribute1.Text = Att1 Attribute1.Visible = True ComboBox1.Visible = True Dim Conn1 As New SqlConnection(ConfigurationManager.ConnectionStrings("ReportingConnectionString").ToString()) Dim Cmd1 As New SqlCommand("SELECT Distinct Attribute FROM Attributes Where AttributeName = @String1", Conn1) Dim String1 As SqlParameter = Cmd1.Parameters.Add("@String1", Data.SqlDbType.VarChar, 48) String1.Value = Att1 Cmd1.CommandType = CommandType.Text Cmd1.Connection.Open() Dim ddlValues1 As SqlDataReader ddlValues1 = Cmd1.ExecuteReader() ComboBox1.DataSource = ddlValues1 ComboBox1.DataValueField = "Attribute" ComboBox1.DataTextField = "Attribute" ComboBox1.DataBind() Cmd1.Connection.Close() Cmd1.Connection.Dispose() ComboBox1.SelectedValue = "Select Value...." End If If Not Att2 = "" Then Attribute2.Text = Att2 Attribute2.Visible = True ComboBox2.Visible = True Dim Conn2 As New SqlConnection(ConfigurationManager.ConnectionStrings("ReportingConnectionString").ToString()) Dim Cmd2 As New SqlCommand("SELECT Distinct Attribute FROM Attributes Where AttributeName = @String2", Conn2) Dim String2 As SqlParameter = Cmd2.Parameters.Add("@String2", Data.SqlDbType.VarChar, 48) String2.Value = Att2 Cmd2.CommandType = CommandType.Text Cmd2.Connection.Open() Dim ddlValues2 As SqlDataReader ddlValues2 = Cmd2.ExecuteReader() ComboBox2.DataSource = ddlValues2 ComboBox2.DataValueField = "Attribute" ComboBox2.DataTextField = "Attribute" ComboBox2.DataBind() Cmd2.Connection.Close() Cmd2.Connection.Dispose() ComboBox2.SelectedValue = "Select Value...." End If
That codes goes on up to att20. It works just fine, but it is forever long, and I would like to clean it up.I want to do something like:
comboboxfill(Att1)
Or, should I pass the combobox number also, so that the code can fill the appropriate combobox? Like.
comboboxfill(Att1,Combobox1)
Any help on how to write the function and how to pass back the information or just how to make it work in general would be apprecitated.Thanks in advance for your help.
Thursday, August 27, 2009 10:27 AM
Answers
-
User-1112770754 posted
Yes, call a function and pass the Combobox to fill and the "attributename". Then use the same connection string... command object, sqlParameter object etc.
I've not tested the below ... but, it should get you in the right direction.
If this helps, pls mark as "Answer".
thanks.
public static void LoadCombo(cboToFill as ComboBox, strAttrib as String)
{
Dim Cmd2 As New SqlCommand("SELECT Distinct Attribute FROM Attributes Where AttributeName = @String2", Conn2)
Dim String2 As SqlParameter = Cmd2.Parameters.Add("@String2", Data.SqlDbType.VarChar, 48)
String2.Value = strAttrib
Cmd2.CommandType = CommandType.Text
Cmd2.Connection.Open()
Dim ddlValues2 As SqlDataReader
ddlValues2 = Cmd2.ExecuteReader()
Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ReportingConnectionString").ToString())
cboToFill.DataSource = ddlValues2
cboToFill.DataValueField = "Attribute"
cboToFill.DataTextField = "Attribute"
cboToFill.DataBind()
}- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 27, 2009 12:29 PM
All replies
-
User-1112770754 posted
Yes, call a function and pass the Combobox to fill and the "attributename". Then use the same connection string... command object, sqlParameter object etc.
I've not tested the below ... but, it should get you in the right direction.
If this helps, pls mark as "Answer".
thanks.
public static void LoadCombo(cboToFill as ComboBox, strAttrib as String)
{
Dim Cmd2 As New SqlCommand("SELECT Distinct Attribute FROM Attributes Where AttributeName = @String2", Conn2)
Dim String2 As SqlParameter = Cmd2.Parameters.Add("@String2", Data.SqlDbType.VarChar, 48)
String2.Value = strAttrib
Cmd2.CommandType = CommandType.Text
Cmd2.Connection.Open()
Dim ddlValues2 As SqlDataReader
ddlValues2 = Cmd2.ExecuteReader()
Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ReportingConnectionString").ToString())
cboToFill.DataSource = ddlValues2
cboToFill.DataValueField = "Attribute"
cboToFill.DataTextField = "Attribute"
cboToFill.DataBind()
}- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 27, 2009 12:29 PM -
User-585544258 posted
I think your code will work, but when I put in cbotofill as ComboBox it underlines it in blue and says type combobox is not defined. If i change it to cbotofill as Dropdownlist it understands it. Any ideas on how to get combobox defined? Or do I just call cbotofill as an object?
Thursday, August 27, 2009 12:49 PM -
User1383809551 posted
Hi,
There is nothin called ComboBox here.. you can use DropDown itself it is equalant to combobox
Thanks
Thursday, August 27, 2009 12:53 PM -
User-585544258 posted
That doesn't seem to work. It says Value of type 'Ajaxcontroltoolkit.combobox' cannot be converted to 'System.Web.UI.Webcontrols.Dropdownlist'.
Thursday, August 27, 2009 1:15 PM -
User-1112770754 posted
a quick fix should be...
public static void LoadCombo(cboToFill as System.Web.UI.Webcontrols.Dropdownlist, strAttrib as String)
etc.
Thursday, August 27, 2009 2:00 PM -
User-585544258 posted
This seemed to work as an object:
Public Function fillcombobox(ByVal cbotofill As Object, ByVal Att As String) As Object Dim Conn1 As New SqlConnection(ConfigurationManager.ConnectionStrings("ReportingConnectionString").ToString()) Dim Cmd1 As New SqlCommand("SELECT Distinct Attribute FROM Attributes Where AttributeName = @String1", Conn1) Dim String1 As SqlParameter = Cmd1.Parameters.Add("@String1", Data.SqlDbType.VarChar, 48) String1.Value = Att Cmd1.CommandType = CommandType.Text Cmd1.Connection.Open() Dim ddlValues1 As SqlDataReader ddlValues1 = Cmd1.ExecuteReader() cbotofill.DataSource = ddlValues1 cbotofill.DataValueField = "Attribute" cbotofill.DataTextField = "Attribute" cbotofill.DataBind() Cmd1.Connection.Close() Cmd1.Connection.Dispose() cbotofill.SelectedValue = "Select Value...." End Function
Then Call it by:
fillcombobox(ComboBox1, Att1)
Thank you very much for your help.
Thursday, August 27, 2009 2:08 PM