?
Solved

Probelm connection to database  asp.net

Posted on 2006-03-30
5
Medium Priority
?
290 Views
Last Modified: 2010-04-17
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.



0
Comment
Question by:pratikshahse
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:rbadaro
ID: 16332718
Hi

What kind of object is drpbrand_nm? Is it initialized?
0
 

Author Comment

by:pratikshahse
ID: 16332741
drpbrand_nm is a dropdown box. how do i intialize it?
0
 
LVL 2

Expert Comment

by:rbadaro
ID: 16332829
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
 

Author Comment

by:pratikshahse
ID: 16332970
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
 
LVL 2

Accepted Solution

by:
rbadaro earned 2000 total points
ID: 16333068
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question