We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Probelm connection to database  asp.net

pratikshahse
pratikshahse asked
on
Medium Priority
303 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.



Comment
Watch Question

Commented:
Hi

What kind of object is drpbrand_nm? Is it initialized?

Author

Commented:
drpbrand_nm is a dropdown box. how do i intialize it?

Commented:
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?

Author

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.

Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.