In my webform1.aspx designer mode, Ive created a sqlconnection and a sqlcommand called cmdCategoriesAll.
The SQL Statement for cmdCategoriesAll is as below:
cmdCategoriesAll = "SELECT categoryID, categoryName, Description FROM Categories"
In the Code view:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim dreader As SqlClient.SqlDataReader
SqlConnection1.Open()
dreader = cmdCategoriesAll.ExecuteReader()
Dim firstrow As Boolean = True
While dreader.Read()
ddlCategoryID.Items.Add(New ListItem(dreader(0).ToString()))
If firstrow Then
txtCategoryName.Text = dreader(1).ToString()
txtCategoryDescription.Text = dreader(2).ToString()
firstrow = False
End If
End While
dreader.Close()
SqlConnection1.Close()
End If
End Sub
When I view the form, therere 3 controls, one dropdown & two textboxes. Now the dropdown is showing the CategoryID. What I would like it to do is to show the CategoryName but bound to CategoryID so that when I insert it into the database, itll insert the CategoryID instead of the CategoryName. To show the CategoryName, I know I just need to change this line to:
ddlCategoryID.Items.Add(New ListItem(dreader(1).ToString())). I know VB 6, I can set the Dropdown listindex to the CategoryID, but in VB.Net, Im not sure how. Can anyone pls help?
Thanks.
The SQL Statement for cmdCategoriesAll is as below:
cmdCategoriesAll = "SELECT categoryID, categoryName, Description FROM Categories"
In the Code view:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim dreader As SqlClient.SqlDataReader
SqlConnection1.Open()
dreader = cmdCategoriesAll.ExecuteReader()
Dim firstrow As Boolean = True
While dreader.Read()
ddlCategoryID.Items.Add(New ListItem(dreader(0).ToString()))
If firstrow Then
txtCategoryName.Text = dreader(1).ToString()
txtCategoryDescription.Text = dreader(2).ToString()
firstrow = False
End If
End While
dreader.Close()
SqlConnection1.Close()
End If
End Sub
When I view the form, therere 3 controls, one dropdown & two textboxes. Now the dropdown is showing the CategoryID. What I would like it to do is to show the CategoryName but bound to CategoryID so that when I insert it into the database, itll insert the CategoryID instead of the CategoryName. To show the CategoryName, I know I just need to change this line to:
ddlCategoryID.Items.Add(New ListItem(dreader(1).ToString())). I know VB 6, I can set the Dropdown listindex to the CategoryID, but in VB.Net, Im not sure how. Can anyone pls help?
Thanks.