Newbie question: DataBinding...

T

The Eeediot

Hello,

I have a drop-down list (listbox) control in ASP.NET that I would like to contain "Select Type" as the first element (text) and "0" as its value then have it followed by values from a reference table. So far I have been using the following sort of code:

Protected WithEvents lstType as ListBox

{Blah, Blah}

'Fill the lstType listbox
Private Sub FillTypeList()
'Empty the list
lstType.Items.Clear()

'Define database objects
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim dreader as SqlDataReader

'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"

'Connect to server and pull out data
Try
'Open connetion
'Import information to DataReader object and
'cough-up into listbox.
conn.Open()
dreader = cmd.ExecuteReader()

'Zeroth list item that says 'Select Me!!!'
Dim ZerothItem as New ListItem()
ZerothItem.Text = "Select Me!!!"
ZerothItem.Value = 0

lstIncdType.Items.Add(ZerothItem)

Do While dreader.Read()
'Populate list
Dim NewItem as New ListItem()
NewItem.Text = dreader("Name")
NewItem.Value = dreader("Idx")
lstIncdType.Items.Add(NewItem)
Loop
dreader.Close()

Catch err as Exception
'To err is human...bail-out!
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message

Finally
'close connection
If NOT(IsNothing(conn)) Then
conn.Close()
End If
End Try
End Sub


How could I use DataBinding with this one?

TIA...
 
K

Karl Seguin

Off the the top of my head, something like this should work (I added caching too, you can take it out if you want)...this is uncompiled code, so might have a couple bugs:

private Function GetGenericType As DataTable
dim cacheKey as string = "AllGenericTypes"
dim dt as DataTable = ctype(HttpRuntime.Cache(cacheKey), DataTable)

if dt is nothing then
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim da as new SqlDataAdapter(cmd)

'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"


Try
dt = new DataTable()
conn.Open()
da.fill(dt)
HttpRuntime.Cache.Insert(cacheKey, dt, nothing, DateTime.Now.AddHours(6), TimeSpan.Zero)
Catch err as Exception
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message
Finally
conn.dispose() //your connection can never be nothing here
cmd.dispose()
da.dispose()
end try
end if
return dt
end function

private sub FillTypeList()
lstType.Items.Clear() //not sure why this is necessary
lstType.DataSource = GetGenericType()
lstType.DataKeyField = "idx"
lstType.DataValueField = "Name"
lstType.DataBind()

lstType.Insert(0, New ListItem("Select Me!!!", "0"))
end sub

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Hello,

I have a drop-down list (listbox) control in ASP.NET that I would like to contain "Select Type" as the first element (text) and "0" as its value then have it followed by values from a reference table. So far I have been using the following sort of code:

Protected WithEvents lstType as ListBox

{Blah, Blah}

'Fill the lstType listbox
Private Sub FillTypeList()
'Empty the list
lstType.Items.Clear()

'Define database objects
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim dreader as SqlDataReader

'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"

'Connect to server and pull out data
Try
'Open connetion
'Import information to DataReader object and
'cough-up into listbox.
conn.Open()
dreader = cmd.ExecuteReader()

'Zeroth list item that says 'Select Me!!!'
Dim ZerothItem as New ListItem()
ZerothItem.Text = "Select Me!!!"
ZerothItem.Value = 0

lstIncdType.Items.Add(ZerothItem)

Do While dreader.Read()
'Populate list
Dim NewItem as New ListItem()
NewItem.Text = dreader("Name")
NewItem.Value = dreader("Idx")
lstIncdType.Items.Add(NewItem)
Loop
dreader.Close()

Catch err as Exception
'To err is human...bail-out!
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message

Finally
'close connection
If NOT(IsNothing(conn)) Then
conn.Close()
End If
End Try
End Sub


How could I use DataBinding with this one?

TIA...
 
T

The Eeediot

Thanks, Dude!

I will give this a try. At least, it will point me in a different direction.
Off the the top of my head, something like this should work (I added caching too, you can take it out if you want)...this is uncompiled code, so might have a couple bugs:

private Function GetGenericType As DataTable
dim cacheKey as string = "AllGenericTypes"
dim dt as DataTable = ctype(HttpRuntime.Cache(cacheKey), DataTable)

if dt is nothing then
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim da as new SqlDataAdapter(cmd)

'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"


Try
dt = new DataTable()
conn.Open()
da.fill(dt)
HttpRuntime.Cache.Insert(cacheKey, dt, nothing, DateTime.Now.AddHours(6), TimeSpan.Zero)
Catch err as Exception
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message
Finally
conn.dispose() //your connection can never be nothing here
cmd.dispose()
da.dispose()
end try
end if
return dt
end function

private sub FillTypeList()
lstType.Items.Clear() //not sure why this is necessary
lstType.DataSource = GetGenericType()
lstType.DataKeyField = "idx"
lstType.DataValueField = "Name"
lstType.DataBind()

lstType.Insert(0, New ListItem("Select Me!!!", "0"))
end sub

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Hello,

I have a drop-down list (listbox) control in ASP.NET that I would like to contain "Select Type" as the first element (text) and "0" as its value then have it followed by values from a reference table. So far I have been using the following sort of code:

Protected WithEvents lstType as ListBox

{Blah, Blah}

'Fill the lstType listbox
Private Sub FillTypeList()
'Empty the list
lstType.Items.Clear()

'Define database objects
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim dreader as SqlDataReader

'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"

'Connect to server and pull out data
Try
'Open connetion
'Import information to DataReader object and
'cough-up into listbox.
conn.Open()
dreader = cmd.ExecuteReader()

'Zeroth list item that says 'Select Me!!!'
Dim ZerothItem as New ListItem()
ZerothItem.Text = "Select Me!!!"
ZerothItem.Value = 0

lstIncdType.Items.Add(ZerothItem)

Do While dreader.Read()
'Populate list
Dim NewItem as New ListItem()
NewItem.Text = dreader("Name")
NewItem.Value = dreader("Idx")
lstIncdType.Items.Add(NewItem)
Loop
dreader.Close()

Catch err as Exception
'To err is human...bail-out!
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message

Finally
'close connection
If NOT(IsNothing(conn)) Then
conn.Close()
End If
End Try
End Sub


How could I use DataBinding with this one?

TIA...
 
T

The Eeediot

For anyone checking this thread out the line should read

lstType.Items.Insert(0, New ListItem("Select Me!!!", "0"))

....but thank you Mr. Seguin.
Off the the top of my head, something like this should work (I added caching too, you can take it out if you want)...this is uncompiled code, so might have a couple bugs:

private Function GetGenericType As DataTable
dim cacheKey as string = "AllGenericTypes"
dim dt as DataTable = ctype(HttpRuntime.Cache(cacheKey), DataTable)

if dt is nothing then
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim da as new SqlDataAdapter(cmd)

'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"


Try
dt = new DataTable()
conn.Open()
da.fill(dt)
HttpRuntime.Cache.Insert(cacheKey, dt, nothing, DateTime.Now.AddHours(6), TimeSpan.Zero)
Catch err as Exception
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message
Finally
conn.dispose() //your connection can never be nothing here
cmd.dispose()
da.dispose()
end try
end if
return dt
end function

private sub FillTypeList()
lstType.Items.Clear() //not sure why this is necessary
lstType.DataSource = GetGenericType()
lstType.DataKeyField = "idx"
lstType.DataValueField = "Name"
lstType.DataBind()

lstType.Insert(0, New ListItem("Select Me!!!", "0"))
end sub

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Hello,

I have a drop-down list (listbox) control in ASP.NET that I would like to contain "Select Type" as the first element (text) and "0" as its value then have it followed by values from a reference table. So far I have been using the following sort of code:

Protected WithEvents lstType as ListBox

{Blah, Blah}

'Fill the lstType listbox
Private Sub FillTypeList()
'Empty the list
lstType.Items.Clear()

'Define database objects
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim dreader as SqlDataReader

'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"

'Connect to server and pull out data
Try
'Open connetion
'Import information to DataReader object and
'cough-up into listbox.
conn.Open()
dreader = cmd.ExecuteReader()

'Zeroth list item that says 'Select Me!!!'
Dim ZerothItem as New ListItem()
ZerothItem.Text = "Select Me!!!"
ZerothItem.Value = 0

lstIncdType.Items.Add(ZerothItem)

Do While dreader.Read()
'Populate list
Dim NewItem as New ListItem()
NewItem.Text = dreader("Name")
NewItem.Value = dreader("Idx")
lstIncdType.Items.Add(NewItem)
Loop
dreader.Close()

Catch err as Exception
'To err is human...bail-out!
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message

Finally
'close connection
If NOT(IsNothing(conn)) Then
conn.Close()
End If
End Try
End Sub


How could I use DataBinding with this one?

TIA...
 
K

Karl

doh! good catch! (guess you kinda had to though :) )

Please don't call me mr seguin :p

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


For anyone checking this thread out the line should read

lstType.Items.Insert(0, New ListItem("Select Me!!!", "0"))

...but thank you Mr. Seguin.
Off the the top of my head, something like this should work (I added caching too, you can take it out if you want)...this is uncompiled code, so might have a couple bugs:

private Function GetGenericType As DataTable
dim cacheKey as string = "AllGenericTypes"
dim dt as DataTable = ctype(HttpRuntime.Cache(cacheKey), DataTable)

if dt is nothing then
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim da as new SqlDataAdapter(cmd)

'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"


Try
dt = new DataTable()
conn.Open()
da.fill(dt)
HttpRuntime.Cache.Insert(cacheKey, dt, nothing, DateTime.Now.AddHours(6), TimeSpan.Zero)
Catch err as Exception
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message
Finally
conn.dispose() //your connection can never be nothing here
cmd.dispose()
da.dispose()
end try
end if
return dt
end function

private sub FillTypeList()
lstType.Items.Clear() //not sure why this is necessary
lstType.DataSource = GetGenericType()
lstType.DataKeyField = "idx"
lstType.DataValueField = "Name"
lstType.DataBind()

lstType.Insert(0, New ListItem("Select Me!!!", "0"))
end sub

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Hello,

I have a drop-down list (listbox) control in ASP.NET that I would like to contain "Select Type" as the first element (text) and "0" as its value then have it followed by values from a reference table. So far I have been using the following sort of code:

Protected WithEvents lstType as ListBox

{Blah, Blah}

'Fill the lstType listbox
Private Sub FillTypeList()
'Empty the list
lstType.Items.Clear()

'Define database objects
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim dreader as SqlDataReader

'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"

'Connect to server and pull out data
Try
'Open connetion
'Import information to DataReader object and
'cough-up into listbox.
conn.Open()
dreader = cmd.ExecuteReader()

'Zeroth list item that says 'Select Me!!!'
Dim ZerothItem as New ListItem()
ZerothItem.Text = "Select Me!!!"
ZerothItem.Value = 0

lstIncdType.Items.Add(ZerothItem)

Do While dreader.Read()
'Populate list
Dim NewItem as New ListItem()
NewItem.Text = dreader("Name")
NewItem.Value = dreader("Idx")
lstIncdType.Items.Add(NewItem)
Loop
dreader.Close()

Catch err as Exception
'To err is human...bail-out!
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message

Finally
'close connection
If NOT(IsNothing(conn)) Then
conn.Close()
End If
End Try
End Sub


How could I use DataBinding with this one?

TIA...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top