hallo,
I'm trying to populate a dropdown by using a stored procedure in an access db, but what I get is completely empty dropdown. Am I doing something wrong?
this is the code I'm using:
code-behind:
Private function BindArchive()
dim myConnection as new OleDBConnection ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\inetpub\wwwroot\new1\db.mdb")
dim objCmd_archive as OleDbCommand = new OleDbCommand ("Query1", myConnection)
objCmd_archive.CommandType = CommandType.StoredProcedure
myConnection.Open()
Return objCmd_archive.ExecuteReader()
myConnection.Close()
End Function
dropdown control:
<form runat="server">
<asp:DropDownList id="cbo_archive" DataSource="<%# BindArchive() %>" DataTextField="mes_date" DataTextValue="mes_date" runat="server" />
</form>
thanks for suggestions
I highly recommend against using this function for getting the data. The reason is that you are exiting the function before closing the database, which you should never do and which means that the database won't be closed. You need to assign the DataReader to the DDL's DataSource in your CodeBehind. For example:
Page_Load(...)
...
myConnection.Open()
cbo_archive.DataSource = objCmd_archive.ExecuteReader();
cbo_archive.DataBind();
myConnection.Close.
End
Furthermore, it is best practice to place you database activity within aTry/Catch/Finally block, where you close the database connection in the Finally section.
0 comments:
Post a Comment