Saturday, March 24, 2012

Populate Drop Down from SQL

Welp, here is a simple one. I'm trying to teach myself asp.net and run into problems here and there...

If I'm not doing this the most efficiant way let me know!

I have a SQL table that contains a list of names, this list may change from time to time, and I plan to have another form to update this list when I get a bit more educated and start fine tuning(:->)

Anyhow with that in mind...I want a drop down list to display items from this SQL database, keeping in mind that I want to post value to another sql table...Once again I may be doing this totally the wrong way, but hey I'm learning!

So what I've managed to do is create a connect, and read the data into a datatable. I read it into the datatable, then bound it to a datagrid so I could see that I actually did it... and it worked!! yippie!!
so now I've removed the data table, and am attempting to use the same type connection to itterate(??) through a for each loop to populate the items in my dropdown list from the data table...

ok blah blah blah huh..here is the code I have, and I'm stuck at how to itterate and display the data, keeping in mind that I want this dropdown to post the text in the dropdown. I may have lots of extras I don't need in there also..hey I'm a newbie at this!
Thanks
Josh

------code--------
Private Sub DropDownManagers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownManagers.SelectedIndexChanged

Dim ConnStr As String
Dim SQL As String
Dim MySqlConn As New SqlConnection(ConnStr)
Dim FinDataSet As New DataSet
Dim FinDataTable As DataTable
Dim FinDataRow As DataRow
Dim FinDataColumn As DataColumn
Dim MySqlAdapter As New SqlDataAdapter(SQL, ConnStr)
Dim MySqlCB As New SqlCommandBuilder(MySqlAdapter)
ConnStr = "server=(local);database=ProjDms; " & _
"Trusted_Connection=yes"
SQL = "SELECT * FROM FinManagers"
FinDataSet.ReadXmlSchema(Server.MapPath("Managers.xsd"))

'Fill the dataset from SQL
MySqlAdapter.Fill(FinDataSet)

For Each FinDataRow In FinDataTable.Rows
'HERE IS MY PROBLEM AS FAR AS I KNOW
Next
End SubYou don't have to iterate. You can databind the dropdownlist to the data directly, provided you tell it which fields are the text and value fields...
I guess I have some more reading to do huh ?? :->


For Each FinDataRow In FinDataTable.Rows
ddlname.Items.Add(new listItem(FinDataRow("FieldName").ToString,FinDataRow("FieldName").ToString) )
Next

or
not iterating


ddlname.DataSource=FinDataTable
ddlname.DataTextField=FinDataTable.Columns("fieldName").ToString
ddlname.DataValueField=FinDataTable.Columns("fieldName").ToString
ddlname.DataBind

Got it working.

I didn't use a direct databind to the sql to populate as I couldn't figure it out...or at least it was easier to figure out how to do it binding to a dataset, as the samples in the book I bought only show binding other types of things to data sets rather than directly to sql, and as I'm a newb, the information about binding the dropdown to the dataset provided in the previous post made the task much easier.

Thanks for the help!!!!!
Problem number 1 solved :-
Josh

0 comments:

Post a Comment