Solved

How to get field name alias to display in drop down while passing the field name as a value

Posted on 2006-11-16
9
236 Views
Last Modified: 2008-02-07
I have the following code to populate a dropdownlist and is passing the parameter to a select statement  but i'd like the alias such as "First Name" to be displayed in the dropdown list but  pass 'fname' as the value of that selectedvalue.   How do i modify the code to make that happen

        If Not Page.IsPostBack Then
            Dim strCon As String = ConfigurationManager.ConnectionStrings("CMConnectionString").ConnectionString
            Dim con As New SqlConnection(strCon)
            Dim sSQL As String = "Select top 1 FName as [First Name], LName as [Last Name], Company, State, Zip, HomePhone as [Home Phone], WorkPhone as [Work Phone], SerialNumber as [Serial#], ProductName as [Model],  HomeEmail as [Email Address] from tblCM"

            Dim cmd As New SqlCommand(sSQL, con)
            Dim oEmptyOption As New ListItem()
            Dim Array1 As New ArrayList
            Try
                con.Open()
                Dim reader As SqlDataReader = cmd.ExecuteReader()
                For intColumn As Integer = 0 To reader.FieldCount - 1
                    Array1.Add(reader.GetName(intColumn))
                Next

                ddSearchField.DataSource = Array1
                ddSearchField.DataBind()
                ddSearchField.Items.Insert(0, oEmptyOption)

                ddSearchField1.DataSource = Array1
                ddSearchField1.DataBind()
                ddSearchField1.Items.Insert(0, oEmptyOption)

                ddSearchField2.DataSource = Array1
                ddSearchField2.DataBind()
                ddSearchField2.Items.Insert(0, oEmptyOption)
            Catch
            Finally
                con.Close()
            End Try
        End If
End sub

-------here is where i get  the value of the dropdownlist selectedvalue --- In this section I aslo wants to return CMId and SourceID but not show them in the gridview ---

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim criteria1 As String = ddSearchField.SelectedValue & ddCriteria.SelectedValue & "'" & TextBox1.Text & "'"
        Dim criteria12 As String = ddCond.SelectedValue & ddSearchField1.SelectedValue & ddCriteria1.SelectedValue & TextBox2.Text
        Dim criteria3 As String = ddCond1.SelectedValue & ddSearchField2.SelectedValue & ddCriteria2.SelectedValue & TextBox3.Text

        Dim strCon As String = ConfigurationManager.ConnectionStrings("CMConnectionString").ConnectionString
        Dim con As New SqlConnection(strCon)
        Dim sSQL As String = "Select CMID, SourceID , SourceName as [Source], FName as [First Name], LName as [Last Name], HomePhone as [Telephone], SerialNumber as [Serial#], Address1 as [Address], City, State, Zip, ProductName as [Product Name]  From vCM where " & criteria1
        Dim cmd As New SqlCommand(sSQL, con)
        Dim myDataSet As New DataSet

        'Create a SqlDataAdapter instance
        Dim myAdapter As New SqlDataAdapter(cmd)
        myAdapter.Fill(myDataSet)

        'Bind the DataSet to the GridView
        GridView1.DataSource = myDataSet
        GridView1.DataBind()

        'Close the connection
        con.Close()

    End Sub
0
Comment
Question by:krbnldy
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
You need to create an ArrayList with objects like DictionaryEntry that has two properties--Key and Value that you can bind the DataTextField and DataValueField to.

Bob
0
 

Author Comment

by:krbnldy
Comment Utility
COuld you show me how that is done.  I am still new at this
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Examples:

1) Add a DictionaryEntry to the ArrayList
    Array1.Add(New DictionaryEntry("Name", "Value"))

2) Bind the DropDownList to the ArrayList

3) Set the DataTextField and DataValueField:

   ddSearchField.DataTextField = "Key"
   ddSearchField.DataValueField = "Value"
 
   The DictionaryEntry class has two properties--Key and Value.  When you create a new DictionaryEntry, the constructors takes two arguments--Key and Value.  

Bob
0
 

Author Comment

by:krbnldy
Comment Utility
I modified my code to look like below and it is not displaying the list,  
I think I am not getting it clearly

Dim reader As SqlDataReader = cmd.ExecuteReader()
                For intColumn As Integer = 0 To reader.FieldCount - 1
                   Array1.Add(reader.GetName(intColumn))
                    Array1.Add(New DictionaryEntry("name", "first name"))
                Next

                ddSearchField.DataSource = Array1
                ddSearchField.DataBind()
                ddSearchField.DataTextField = "Key"
                ddSearchField.DataValueField = "Value"
                ddSearchField.Items.Insert(0, oEmptyOption)
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 96

Accepted Solution

by:
Bob Learned earned 75 total points
Comment Utility
Try this:

                Dim reader As SqlDataReader = cmd.ExecuteReader()
                For intColumn As Integer = 0 To reader.FieldCount - 1
                    Array1.Add(New DictionaryEntry("name", "first name"))
                Next

                ddSearchField.DataSource = Array1
                ddSearchField.DataTextField = "Key"
                ddSearchField.DataValueField = "Value"
                ddSearchField.Items.Insert(0, oEmptyOption)
                ddSearchField.DataBind()

The DataBind call needs to be the last thing.

Bob
0
 

Author Comment

by:krbnldy
Comment Utility
This works with one problem.  My dropdown list has only one value that is first name.

I added the following but it still has only one value,   How do i make it show all the values in the sql statement

                    Array1.Add(New DictionaryEntry("First Name", "fname"))
                    Array1.Add(New DictionaryEntry("Last Name", "lname"))
                    Array1.Add(New DictionaryEntry("Company", "Company"))
                    Array1.Add(New DictionaryEntry("State", "State"))
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Are you trying to add those properties from the data reader?

Bob
0
 

Author Comment

by:krbnldy
Comment Utility
Yes i am

I tried cahnging the array to

                    Array1.Add(New DictionaryEntry(reader.GetName(intColumn), value))
 but got the list but not the value of each fiield
0
 

Author Comment

by:krbnldy
Comment Utility
Think I got it

                    Array1.Add(New DictionaryEntry(reader.GetName(0), "fname"))
                    Array1.Add(New DictionaryEntry(reader.GetName(1), "lname"))
                    Array1.Add(New DictionaryEntry(reader.GetName(2), "Company"))
                    Array1.Add(New DictionaryEntry(reader.GetName(3), "State"))
                    Array1.Add(New DictionaryEntry(reader.GetName(4), "Zip"))
                    Array1.Add(New DictionaryEntry(reader.GetName(5), "HomePhone"))
                    Array1.Add(New DictionaryEntry(reader.GetName(6), "WorkPhone"))
Thank you for your help
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now