Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. help!!! its about saving data using codes

help!!! its about saving data using codes

Scheduled Pinned Locked Moved ASP.NET
helpquestiondatabaselearning
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rharzkie
    wrote on last edited by
    #1

    in saving data in my module, instead of using connection wizard, i use this sql codes: Dim str As String dbconnect = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Rara's project\grading.mdb") dbconnect.Open() str = "select count(*) from students where studname = '" & txtname.Text & "'" dbcommand = New OleDbCommand(str, dbconnect) If dbcommand.ExecuteScalar <> 0 Then MessageBox.Show("Already exists.", "Result") cleartxt() Else str = "Insert into students(studname, year, course) values ('" & txtname.Text & "', " & "'" & cmbyear.Text & "', " & "'" & cmbcourse.Text & "')" dbcommand = New OleDbCommand(str, dbconnect) **dbcommand.ExecuteNonQuery()** MessageBox.Show("Record Added.", "Result") dbconnect.Close() cleartxt() End If but i have an error in the part there -"dbcommand.ExecuteNonQuery()" a dialog box appear and it says: An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll i guess the error there is the combobox that i used? because when i try to erase the "cmbcourse.text" and "cmbyear.text", it runs..but when i put it, there's an error.... how can i fix it??? please, help me!!!

    F 1 Reply Last reply
    0
    • R Rharzkie

      in saving data in my module, instead of using connection wizard, i use this sql codes: Dim str As String dbconnect = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Rara's project\grading.mdb") dbconnect.Open() str = "select count(*) from students where studname = '" & txtname.Text & "'" dbcommand = New OleDbCommand(str, dbconnect) If dbcommand.ExecuteScalar <> 0 Then MessageBox.Show("Already exists.", "Result") cleartxt() Else str = "Insert into students(studname, year, course) values ('" & txtname.Text & "', " & "'" & cmbyear.Text & "', " & "'" & cmbcourse.Text & "')" dbcommand = New OleDbCommand(str, dbconnect) **dbcommand.ExecuteNonQuery()** MessageBox.Show("Record Added.", "Result") dbconnect.Close() cleartxt() End If but i have an error in the part there -"dbcommand.ExecuteNonQuery()" a dialog box appear and it says: An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll i guess the error there is the combobox that i used? because when i try to erase the "cmbcourse.text" and "cmbyear.text", it runs..but when i put it, there's an error.... how can i fix it??? please, help me!!!

      F Offline
      F Offline
      Fred_Smith
      wrote on last edited by
      #2

      Is the year filed in your database a text field, or a nyumeric one? If the latter, you need to remove the apostrophes around it in your sql statement. If any of your courses have apostrophes in them, this too will break the code - use the replace function to replace all occurrences of apostrophes with double-apostrophes.

      R 1 Reply Last reply
      0
      • F Fred_Smith

        Is the year filed in your database a text field, or a nyumeric one? If the latter, you need to remove the apostrophes around it in your sql statement. If any of your courses have apostrophes in them, this too will break the code - use the replace function to replace all occurrences of apostrophes with double-apostrophes.

        R Offline
        R Offline
        Rharzkie
        wrote on last edited by
        #3

        all my database field are text.... the apostrophes that are you saying are in my codes? So i need to remove it? This is my code before: str = "Insert into students(studname, year, course) values ('" & txtname.Text & "', " & "'" & txtyear.Text & "', " & "'" & txtcourse.Text & "')" how can i remove it? Like this? str = "Insert into students(studname, year, course) values ('" & txtname.Text & "', " & " " & txtyear.Text & ", " & " " & txtcourse.Text & ")" but when i do it, it is still dont running? Can u please write the codes for that, so i can know which one is to be removed?

        F 1 Reply Last reply
        0
        • R Rharzkie

          all my database field are text.... the apostrophes that are you saying are in my codes? So i need to remove it? This is my code before: str = "Insert into students(studname, year, course) values ('" & txtname.Text & "', " & "'" & txtyear.Text & "', " & "'" & txtcourse.Text & "')" how can i remove it? Like this? str = "Insert into students(studname, year, course) values ('" & txtname.Text & "', " & " " & txtyear.Text & ", " & " " & txtcourse.Text & ")" but when i do it, it is still dont running? Can u please write the codes for that, so i can know which one is to be removed?

          F Offline
          F Offline
          Fred_Smith
          wrote on last edited by
          #4

          No - your original SQL statement is correct in as far as it goes, but will crash if any of your text inputs include apostrophes. Because SQL uses apostrophes to mark the start and end of text fields, another one occuring within a field causes problems. To get round this, SQL accepts two consecutive apostrophes as marking not the end of the text field but an apostrophy within it - thus if your txtcourse.text = "smith's" the SQL statemt would fail, "smith''s" would be accepted. To take acount for this in all cases, simply use the replace method to replace all apostrpohes within text fields with double apostrophes - thus: str = "Insert into students (studname, year, course) values ('" & txtname.Text.Replace("'", "''") & "', '" & txtyear.Text.Replace("'", "''") & "', '" & txtcourse.Text & "')" cheers Fred

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups