944,134 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 5225
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 15th, 2006
0

Error connecting to database

Expand Post »
I am having problem in connecting to database. The problem is with the following code.

ASP.NET Syntax (Toggle Plain Text)
  1. dim objCmd as new OleDbDataAdapter()
  2. objCmd.SelectCommand.Connection = dbconn
  3. objCmd.SelectCommand.CommandText= sql

I am having error on line2

the error is

System.NullReferenceException: Object variable or With block variable not set.

I think there is problem in my connection string but i cant figure out.

the connection string is

ASP.NET Syntax (Toggle Plain Text)
  1. dim strConnection as String="Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("mydb.mdb")
  2. Dim dbconn as New OleDbConnection(strConnection)

i have also tried

ASP.NET Syntax (Toggle Plain Text)
  1. dim dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=c:/inetpub/wwwroot/day1/mydb.mdb")

but still its not working. I have also seen some tutorials but they didnt help me. Can anyone plz tell me what could be wrong. I want to insert values into database from textboxes.
But i am facing this connectivity error.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Reham Ejaz is offline Offline
23 posts
since Nov 2005
Jan 24th, 2006
0

Re: Error connecting to database

Pls make sure abt "Connection String" for connect to MS Access database at http://www.connectionstrings.com
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wijitstyp is offline Offline
1 posts
since Jan 2006
Jan 24th, 2006
0

Re: Error connecting to database

Thanx. I visited the site Connection string is right. But i am still unable to insert or delete values from database.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Reham Ejaz is offline Offline
23 posts
since Nov 2005
Jan 24th, 2006
0

Re: Error connecting to database

two things:
1) if you can read but can't update or delete, may have a problems with settings on the access database. Right-click on it. Go to security tab. make sure the internet guest account user has read and write privileges checked.
2) if you can't even read, try setting up a dsn connection. Let me know if you need help with that.
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Jan 30th, 2006
0

Error in updating database

There wasnt any problem with the settings. Now I have succeeded in updating simple database via OleDBDataAdapter and OleDbCommandBuilder objects but the same code is not working when I am using to update too many columns. Actually I cant figure out the error in my following code.


ASP.NET Syntax (Toggle Plain Text)
  1. dim conn as new oledbconnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Inetpub\wwwroot\website\visitor.mdb")
  2. Dim cmd As OleDbDataAdapter
  3. Dim ds As New DataSet()
  4. Dim dr As DataRow

ASP.NET Syntax (Toggle Plain Text)
  1. sub submit_clicked(Sender as object, e as Eventargs)
  2.  
  3. dim i,j as integer
  4. dim str as string
  5. 'dim countrystr, tourstr as string
  6. dim param(7) as string
  7. dim flag as boolean=true
  8.  
  9.  
  10. 'Inserting values from text boxes into a string array
  11.  
  12. j=0
  13. for i=0 to panel1.controls.count-1
  14. if panel1.controls(i).GetType Is GetType(textbox) then
  15. str=Ctype(panel1.controls(i),textbox).text
  16. if str <> "" then
  17. param(j)=str
  18. else
  19. flag=false
  20. label1.text="forgot to enter value for " & panel1.controls(i).id
  21. end if
  22. j=j+1
  23. end if
  24. next
  25.  
  26. if not flag then
  27. exit sub
  28. end if
  29.  
  30.  
  31. try
  32. cmd = New OleDbDataAdapter(New OleDbCommand("select * from visitors",conn))
  33. Dim comb As OleDbCommandBuilder = New OleDbCommandBuilder(cmd)
  34. conn.Open()
  35. cmd.fill(ds,"visitorinfo1")
  36. dr = ds.Tables("visitors").NewRow()
  37.  
  38. catch ex as exception
  39. label1.text="Error in updating database"
  40. end try
  41.  
  42. 'Entering values in datarow from textboxes
  43.  
  44. dr("Name") = param(0)
  45. dr("Email") = param(1)
  46.  
  47. if rdbgender.SelectedIndex = 0
  48. dr("Gender") = "Male"
  49. else
  50. dr("Gender") = "Female"
  51. end if
  52.  
  53. dr("Age") = param(2)
  54. dr("Address") = param(3)
  55. dr("Phone No") = param(4)
  56. countrystr = ddlcountry.SelectedItem.Text
  57. dr("Country") = countrystr
  58. dr("Tour Type") = rdbtour.SelectedItem.Text
  59. tourstr = lbdetails.SelectedItem.Text
  60. dr("Tour Detail") = tourstr
  61.  
  62. dr("Custom Tour") = param(5)
  63. dr("No of Travellers") = param(6)
  64.  
  65.  
  66. ds.Tables("visitors").Rows.Add(dr)
  67.  
  68. cmd.Update(ds, "visitors")
  69. label1.text="database updated"
  70.  
  71. end sub
  72.  

Name, Age, ...... are the names of columns in database table in Access

ddlcountry and ddldetails are the id's of dropdownlist controls
rdbtour is id of the radiobutton control in html part of page.


Now with this code I am having problem on the line

cmd.Update(ds, "visitor")

Error is
Syntax error in INSERT INTO statement

If anyone can figureout the error in this code plz let me know. I will be thankful.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Reham Ejaz is offline Offline
23 posts
since Nov 2005
Jan 30th, 2006
0

Re: Error connecting to database

you havent written an insert, update or delete query. Just a select query. your cmd is a "select" command it ONLY knows about getting data. You need a new command for each of the CRUD actions you intend to do (you should in theory write one for each but if you know 100% you will never need a delete then you can leave it out). So you cannot call Update on a command whos SQL is a SELECT query.

Create a new command but this time an Insert Command (with Inser INTO sql or the name of an insert stored proc or query in your database.

I would recommend you use a data adapter and fill all 4 commands in and use this to do everything you need.

If you need more explanation let me know
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Jan 31st, 2006
0

Re: Error connecting to database

Quote originally posted by f1 fan ...
you havent written an insert, update or delete query. Just a select query. your cmd is a "select" command it ONLY knows about getting data. You need a new command for each of the CRUD actions you intend to do (you should in theory write one for each but if you know 100% you will never need a delete then you can leave it out). So you cannot call Update on a command whos SQL is a SELECT query.

Create a new command but this time an Insert Command (with Inser INTO sql or the name of an insert stored proc or query in your database.

I would recommend you use a data adapter and fill all 4 commands in and use this to do everything you need.

If you need more explanation let me know
The command builder object sees the changes in our data set and automatically makes the changes accordingly in the datastore. So with it we dont need insert statement.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Reham Ejaz is offline Offline
23 posts
since Nov 2005
Jan 31st, 2006
0

Re: Error connecting to database

Quote originally posted by Reham Ejaz ...
The command builder object sees the changes in our data set and automatically makes the changes accordingly in the datastore. So with it we dont need insert statement.
ok now i would appreciate any more help. I have tried it with insert statement as well but strill its not working.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Reham Ejaz is offline Offline
23 posts
since Nov 2005
Jan 31st, 2006
0

Re: Error connecting to database

Quote ...
The command builder object sees the changes in our data set and automatically makes the changes accordingly in the datastore. So with it we dont need insert statement.
You misunderstood that. The data adapter sees the rows that have been changed (it actually iterates through each table and checks the row state) and depending on whether the row state is one of added, modified or deleted it calls the appropriate command to do the work.
If you take a step back to think about it... it is impossible for the command to know which value you want to go where. It knows nothing about your database (it only knows to pass some string to the database - the command does not execute it nor check it - you could have set the string as "send me ten million us dollars and a pizza" and it would only fail when you tried to execute it (the database would fail not the command)). So you have to tell it. Trust me, i have been doing this for a long time and spend a lot of time writing each of the stored procs for SQL (well i dont - i have written a generator, but the generator spends a lot of time writing them and i spend a lot of time telling the adapter each one).

You have to write it. Humor me and try it.
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Feb 1st, 2006
0

Re: Error connecting to database

Quote originally posted by f1 fan ...
You misunderstood that. The data adapter sees the rows that have been changed (it actually iterates through each table and checks the row state) and depending on whether the row state is one of added, modified or deleted it calls the appropriate command to do the work.
If you take a step back to think about it... it is impossible for the command to know which value you want to go where. It knows nothing about your database (it only knows to pass some string to the database - the command does not execute it nor check it - you could have set the string as "send me ten million us dollars and a pizza" and it would only fail when you tried to execute it (the database would fail not the command)). So you have to tell it. Trust me, i have been doing this for a long time and spend a lot of time writing each of the stored procs for SQL (well i dont - i have written a generator, but the generator spends a lot of time writing them and i spend a lot of time telling the adapter each one).

You have to write it. Humor me and try it.
But It did work. I mean the code I have given. The only problem now is that its working with some tables but not with all. I dont know what's the problem. I have made a table with personal user entries and its working fine. But when I make a new table with all the entries along with tour details. It gives an error on the update statement as I mentioned earlier. If it works with 1 table it should work with all. I have even tried using insert statement but it gives an error indicating syntax error in insert into statement the same error i get in update statement. Can u help me with that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Reham Ejaz is offline Offline
23 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Bug? ToggleButton problem in toolbar for VC++.NET
Next Thread in ASP.NET Forum Timeline: Need help in project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC