hard code data bind

  • Thread starter Thread starter si_owen
  • Start date Start date
S

si_owen

Hi all,

firstly this is a web apps project.

Right i had a dropdown list which was populated by a db table which was
databound using a sql datasource.

however when adding validation controls the dropdownlist was failing as
an item was already selected. I need a default value in there "please
select one" to appear when the page is loaded or postback occurs. This
dropdownlist item cannot appear in the db but must appear in the list
and appear first before the other items populated by the db.

I have deleted the databond between sql datasource and the dropdownlist
and have written a select statement to grab the categories. i need to
add the "please select one" item before the db is called and then loop
untill all records from the db have been populated into the drop down
list.

all i have so far is below:

Protected Sub DropDownList1_DataBinding(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.DataBinding

Dim SelectCategory As New SqlCommand
Dim ConCategory As New
SqlConnection(dataCategories.ConnectionString)
Dim Param As New SqlParameter("@CategoryID",
Data.SqlDbType.Int)

DropDownList1.Items.Add("Please Select One")

SelectCategory.CommandText = "SELECT category FROM [faq
category] WHERE categoryID = @categoryid"
ConCategory.Open()

Dim reader As SqlDataReader = SelectCategory.ExecuteReader
While reader.Read
DropDownList1.SelectedItem.Value = reader(0)
End While
ConCategory.Close()

End Sub

probably going the wrong way with but thats why i need your help.

many thanks

simon
 
Have you tried setting the text of the combobox to "Select One" then it
should show as default.

--
Thiele Enterprises - The Power Is In Your Hands Now!

--
Hi all,

firstly this is a web apps project.

Right i had a dropdown list which was populated by a db table which was
databound using a sql datasource.

however when adding validation controls the dropdownlist was failing as
an item was already selected. I need a default value in there "please
select one" to appear when the page is loaded or postback occurs. This
dropdownlist item cannot appear in the db but must appear in the list
and appear first before the other items populated by the db.

I have deleted the databond between sql datasource and the dropdownlist
and have written a select statement to grab the categories. i need to
add the "please select one" item before the db is called and then loop
untill all records from the db have been populated into the drop down
list.

all i have so far is below:

Protected Sub DropDownList1_DataBinding(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.DataBinding

Dim SelectCategory As New SqlCommand
Dim ConCategory As New
SqlConnection(dataCategories.ConnectionString)
Dim Param As New SqlParameter("@CategoryID",
Data.SqlDbType.Int)

DropDownList1.Items.Add("Please Select One")

SelectCategory.CommandText = "SELECT category FROM [faq
category] WHERE categoryID = @categoryid"
ConCategory.Open()

Dim reader As SqlDataReader = SelectCategory.ExecuteReader
While reader.Read
DropDownList1.SelectedItem.Value = reader(0)
End While
ConCategory.Close()

End Sub

probably going the wrong way with but thats why i need your help.

many thanks

simon
 
yes that was the first thing i tried, but the drag and drop data bind
over rides cjanging the text property in the properties.
 
Back
Top