I created a VB file in the App_Code folder, made a Namespace, made a public class inside and put most of my code there. Then I call that code in codebehind, mostly in the PageLoad. So far so good.
Now, I'm having problems with the sub that I use to fill a DropDown. This is my code:
Public Sub myDropDownCode()
Dim connStr As String = ConfigurationManager.ConnectionStrings("connname").ConnectionString
Dim cnn As New SqlConnection(connStr)
cnn.Open()
Try
Dim DBCmd As New SqlCommand
DBCmd.Connection = cnn
DBCmd.CommandType = CommandType.StoredProcedure
DBCmd.CommandText = "myStoredProcedures"
Dim rd As SqlDataReader
rd = DBCmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch exp As Exception
'Response.Write(exp)
Finally
cnn.Close()
End Try
End Sub
I have no idea how to modify this code so that I may call it in code behind from the NameSpace. I can make a reader and read a storedprocedure, no problems with that, but in this case since I have to include the DataTextField and the DataValueField, I'm
totally lost.
Namespace myNameSpace
Public Class Class1
Public Shared Sub bindMyDropDownList(DDExpensesTypes As DropDownList)
Dim connStr As String = ConfigurationManager.ConnectionStrings("connname").ConnectionString
Dim cnn As New SqlConnection(connStr)
cnn.Open()
Try
Dim DBCmd As New SqlCommand
DBCmd.Connection = cnn
DBCmd.CommandType = CommandType.StoredProcedure
DBCmd.CommandText = "myStoredProcedures"
Dim rd As SqlDataReader
rd = DBCmd.ExecuteReader(CommandBehavior.CloseConnection)
DDExpensesTypes.DataSource = rd
DDExpensesTypes.DataTextField = "Information"
DDExpensesTypes.DataValueField = "ID"
DDExpensesTypes.DataBind()
Catch exp As Exception
'Response.Write(exp)
Finally
cnn.Close()
End Try
End Sub
End Class
End Namespace
To call from code behind1) Add namespace: In this case I have created myNameSpace.
2) From the point you want to bind the drop down do like
Diomedes
Member
202 Points
145 Posts
How to move code to Fill a DropDown from code behind to App_Code (NameSpace)
Nov 27, 2012 03:11 AM|LINK
Hi,
I created a VB file in the App_Code folder, made a Namespace, made a public class inside and put most of my code there. Then I call that code in codebehind, mostly in the PageLoad. So far so good.
Now, I'm having problems with the sub that I use to fill a DropDown. This is my code:
Public Sub myDropDownCode()
Dim connStr As String = ConfigurationManager.ConnectionStrings("connname").ConnectionString
Dim cnn As New SqlConnection(connStr)
cnn.Open()
Try
Dim DBCmd As New SqlCommand
DBCmd.Connection = cnn
DBCmd.CommandType = CommandType.StoredProcedure
DBCmd.CommandText = "myStoredProcedures"
Dim rd As SqlDataReader
rd = DBCmd.ExecuteReader(CommandBehavior.CloseConnection)
DDExpensesTypes.DataSource = rd
DDExpensesTypes.DataTextField = "Information"
DDExpensesTypes.DataValueField = "ID"
DDExpensesTypes.DataBind()
Catch exp As Exception
'Response.Write(exp)
Finally
cnn.Close()
End Try
End Sub
I have no idea how to modify this code so that I may call it in code behind from the NameSpace. I can make a reader and read a storedprocedure, no problems with that, but in this case since I have to include the DataTextField and the DataValueField, I'm totally lost.
Any advice will be appriciated.
Thanks!
anuj_koundal
Contributor
2088 Points
496 Posts
Re: How to move code to Fill a DropDown from code behind to App_Code (NameSpace)
Nov 27, 2012 03:31 AM|LINK
Do like this
1) Create a class with namespace
Namespace myNameSpace Public Class Class1 Public Shared Sub bindMyDropDownList(DDExpensesTypes As DropDownList) Dim connStr As String = ConfigurationManager.ConnectionStrings("connname").ConnectionString Dim cnn As New SqlConnection(connStr) cnn.Open() Try Dim DBCmd As New SqlCommand DBCmd.Connection = cnn DBCmd.CommandType = CommandType.StoredProcedure DBCmd.CommandText = "myStoredProcedures" Dim rd As SqlDataReader rd = DBCmd.ExecuteReader(CommandBehavior.CloseConnection) DDExpensesTypes.DataSource = rd DDExpensesTypes.DataTextField = "Information" DDExpensesTypes.DataValueField = "ID" DDExpensesTypes.DataBind() Catch exp As Exception 'Response.Write(exp) Finally cnn.Close() End Try End Sub End Class End NamespaceTo call from code behind1) Add namespace: In this case I have created myNameSpace.
2) From the point you want to bind the drop down do like
Regards
Anuj Koundal
Diomedes
Member
202 Points
145 Posts
Re: How to move code to Fill a DropDown from code behind to App_Code (NameSpace)
Nov 27, 2012 02:42 PM|LINK
Thanks!

I was just missing the "(DDExpensesTypes As DropDownList)" on the sub name. I should had know that.