Probelm connection to database asp.net

I am trying to connect to database in asp.net form by coding my connection rather than using the asp.net in built components. here is my code.

Dim myconnection As OleDbConnection
            Dim mycommand As OleDbDataAdapter
            Dim mydataset As DataSet
            Dim mytable As DataTable
            Dim sql As String
            Dim loop1, numrows As Integer


            'Me.OleDbCommand1.CommandText = "SELECT brand_name FROM brand"
            'OleDbDataAdapter1.Fill(DataSet1)
            'drpbrand_nm.DataSource = DataSet1
            'drpbrand_nm.DataBind()
            'brand = drpbrand_nm.SelectedValue

            sql = " select brand_name from brand"
            mycommand = New OleDbDataAdapter(sql, conn)
            mydataset = New DataSet
            mycommand.Fill(mydataset)
            drpbrand_nm.DataSource = mydataset
            drpbrand_nm.DataBind()   -------------------------------> There is a drop down on my form which I am trying to populate using this code
            numrows = mytable.Rows.Count-----------------------> Errors out over here . See the error at bottom

           For loop1 = 0 To numrows - 1

                drpbrand_nm.SelectedValue = mytable.Rows(loop1).Item("brand_name")

            Next loop1

Here is the error I get. "Object reference not set to an instance of an object". So it sounds like it is not connecting to the table. until now I was used to setting up connections using dataadapters, dataset and command from asp.net in built components. But for this project I am not allowed to use those . So please help me.



pratikshahseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rbadaroCommented:
Hi

What kind of object is drpbrand_nm? Is it initialized?
0
pratikshahseAuthor Commented:
drpbrand_nm is a dropdown box. how do i intialize it?
0
rbadaroCommented:
Sorry, I didn't see it clearly

your problem is that your DataTable is not initialized. You can initialize mytable setting it to New DataTable.
However, your code doesn't make much sense to me because you have a new DataTable and you are counting it's rows (will be 0, after initialized) and after that you are looping myTable (won't even enter the loop because it has no rows) and you are always setting the selected value of the dropDown.

What do you really want to do?
0
pratikshahseAuthor Commented:
ok here is my updated code.

If Not IsPostBack = True Then
            Dim brand As String
            Dim myconnection As OleDbConnection
            Dim mycommand As OleDbDataAdapter
            Dim mydataset As DataSet
            Dim mytable As DataTable
            Dim sql As String
            Dim loop1, numrows As Integer


            'Me.OleDbCommand1.CommandText = "SELECT brand_name FROM brand"
            'OleDbDataAdapter1.Fill(DataSet1)
            'drpbrand_nm.DataSource = DataSet1
            'drpbrand_nm.DataBind()
            'brand = drpbrand_nm.SelectedValue

            sql = " select brand_name from brand"
            mycommand = New OleDbDataAdapter(sql, conn)
            mydataset = New DataSet
            mycommand.Fill(mydataset)
            drpbrand_nm.DataSource = mydataset
            drpbrand_nm.DataBind()
            mytable = New DataTable
            mytable = mydataset.Tables(0)
            numrows = mytable.Rows.Count

            For loop1 = 0 To numrows - 1

                drpbrand_nm.DataTextField = mytable.Rows(loop1).Item("brand_name")

            Next loop1

It works fine but I dont see "brandname" from brand table in my drop down box. Insteat I see system.data.datarowview. What am I missing so that I can actually see "brandname" from my table.

0
rbadaroCommented:
So what you want is to fill the DropDown with the brand_name field of the brand table.Right? For this you won't need a DataTable, just do this.

If Not IsPostBack = True Then
            Dim brand As String
            Dim myconnection As OleDbConnection
            Dim mycommand As OleDbDataAdapter
            Dim mydataset As DataSet
            Dim sql As String
            Dim loop1, numrows As Integer


            'Me.OleDbCommand1.CommandText = "SELECT brand_name FROM brand"
            'OleDbDataAdapter1.Fill(DataSet1)
            'drpbrand_nm.DataSource = DataSet1
            'drpbrand_nm.DataBind()
            'brand = drpbrand_nm.SelectedValue

            sql = " select brand_name from brand"
            mycommand = New OleDbDataAdapter(sql, conn)
            mydataset = New DataSet
            mycommand.Fill(mydataset)
            drpbrand_nm.DataSource = mydataset
            drpbrand_nm.DataValueField="brand_name" -------> use brand_name as value of the dropdown
            drpbrand_nm.DataTextField="brand_name" ---------> use brand_name as text of the dropdown
            drpbrand_nm.DataBind()


It would probably be best to use an ID field for the DataValueField.

Hope it helped.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.