pratikshahse
asked on
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.CommandT ext = "SELECT brand_name FROM brand"
'OleDbDataAdapter1.Fill(Da taSet1)
'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.
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.CommandT
'OleDbDataAdapter1.Fill(Da
'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() --------------------------
numrows = mytable.Rows.Count--------
For loop1 = 0 To numrows - 1
drpbrand_nm.SelectedValue = mytable.Rows(loop1).Item("
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.
ASKER
drpbrand_nm is a dropdown box. how do i intialize it?
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?
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?
ASKER
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.CommandT ext = "SELECT brand_name FROM brand"
'OleDbDataAdapter1.Fill(Da taSet1)
'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.
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.CommandT
'OleDbDataAdapter1.Fill(Da
'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("
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.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
What kind of object is drpbrand_nm? Is it initialized?