Results 1 to 17 of 17

Thread: Ado .net Insert Into [resolved]

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Ado .net Insert Into [resolved]

    I have problems with the folowing code, and I don't know what's wrong with it...
    VB Code:
    1. Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("ProjectDB.mdb") & ";"
    2. Dim cn As OleDbConnection
    3. cn = New OleDbConnection(connString)
    4. cn.Open()
    5.  
    6. Dim sql_ins As String = "INSERT INTO t_user (userid, username, password) VALUES (2, 'test', 'some password');"
    7. Dim cmd_ins As OleDbCommand
    8. cmd_ins = New OleDbCommand(sql_ins, cn)
    9. cmd_ins.ExecuteNonQuery()
    If I try to do select statements, it works fine, I can retreive the data, but it does not work for insert...
    Last edited by CVMichael; Mar 18th, 2003 at 04:52 PM.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    telling us what error or what happens is helpful.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    this is what I get when I do exception.Message

    System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr)
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
    at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
    at dforums.NewUser.btnSubmit_Click(Object sender, EventArgs e) in C:\Inetpub\Public\dforums\NewUser.aspx.vb:line 96

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Try change password to [password] in the statement
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    nope, same thing.... I even did that for all of them [userid] and [username], and still same error...

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    well userid an username wouldnt have been an issue, only password since it is some kind of reserved word for access.

    Try removing the ; then, maybe it doesnt like that as well as [password]
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    I removed the ; and still same error...

    This is the t_user table viewed in Access, only the first 3 fields are required, the rest are not required and Allow Zero Length is set to Yes


  8. #8
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    paste your code exactly as you have it now. something else is happening
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    I renamed the field password to upassword... (still same error)
    ps: this is a web application
    VB Code:
    1. Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    2.         If txtPassword.Text <> "d41d8cd98f00b204e9800998ecf8427e" AndAlso txtUserName.Text.Length > 0 Then
    3.             Dim cn As OleDbConnection = Session("Connection")
    4.             Dim cmd_chk As OleDbCommand
    5.             Dim dr_chk As OleDbDataReader
    6.  
    7.             Try
    8.                 If cn.State <> ConnectionState.Open Then cn.Open()
    9.  
    10.                 cmd_chk = New OleDbCommand("SELECT username FROM t_user where username = '" & txtUserName.Text & "'", cn)
    11.                 dr_chk = cmd_chk.ExecuteReader()
    12.  
    13.                 If dr_chk.Read() Then
    14.                     Response.Write("<http><body>" & vbNewLine)
    15.                     Response.Write("Username already taken.<br>" & vbNewLine)
    16.                     Response.Write("<a href=""NewUser.aspx"">Click here to go back and try again</a>" & vbNewLine)
    17.                     Response.Write("</body></html>")
    18.                     Response.End()
    19.                 Else
    20.                     If Not dr_chk Is Nothing Then dr_chk.Close()
    21.                     If Not cmd_chk Is Nothing Then cmd_chk.Dispose()
    22.  
    23.                     'Dim sql_ins As String = "INSERT INTO t_user (userid, username, password, email, " & _
    24.                     '    "signature, webpage, occupation, interests, location, biography, date_registered) " & _
    25.                     '    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
    26.                     Dim sql_ins As String = "INSERT INTO t_user (userid, username, upassword) " & _
    27.                         "VALUES (2, 'test', 'some password')"
    28.  
    29.                     Dim cmd_ins As OleDbCommand
    30.                     'Dim par As OleDbParameter
    31.  
    32.                     Try
    33.                         'Dim trn As OleDbTransaction = cn.BeginTransaction()
    34.                         cmd_ins = New OleDbCommand(sql_ins, cn)
    35.                         'trn.Begin()
    36.  
    37.                         'With cmd_ins
    38.                             '.Parameters.Clear()
    39.                             '.Parameters.Add(New OleDb.OleDbParameter("@userid", OleDbType.Numeric))
    40.                             '.Parameters.Add(New OleDb.OleDbParameter("@username", OleDbType.VarChar, 15))
    41.                             '.Parameters.Add(New OleDb.OleDbParameter("@password", OleDbType.VarChar, 32))
    42.  
    43.                             '.Parameters.Add(New OleDb.OleDbParameter("@userid", 2))
    44.                             '.Parameters.Add(New OleDb.OleDbParameter("@username", txtUserName.Text))
    45.                             '.Parameters.Add(New OleDb.OleDbParameter("@password", txtPassword.Text))
    46.                             '.Parameters.Add(New OleDb.OleDbParameter("@email", OleDbType.VarChar, 128))
    47.                             '.Parameters.Add(New OleDb.OleDbParameter("@signature", OleDbType.VarChar, 255))
    48.  
    49.                             '.Parameters.Add(New OleDb.OleDbParameter("@webpage", OleDbType.VarChar, 255))
    50.                             '.Parameters.Add(New OleDb.OleDbParameter("@occupation", OleDbType.VarChar, 100))
    51.                             '.Parameters.Add(New OleDb.OleDbParameter("@interests", OleDbType.VarChar, 255))
    52.                             '.Parameters.Add(New OleDb.OleDbParameter("@location", OleDbType.VarChar, 100))
    53.                             '.Parameters.Add(New OleDb.OleDbParameter("@biography", OleDbType.VarChar, 255))
    54.                             '.Parameters.Add(New OleDb.OleDbParameter("@date_registered", OleDbType.Date))
    55.  
    56.                             '.Parameters("@userid").Value = 2
    57.                             '.Parameters("@username").Value = txtUserName.Text
    58.                             '.Parameters("@password").Value = txtPassword.Text
    59.                             '.Parameters("@date_registered").Value = Now
    60.                             'End With
    61.                         cmd_ins.ExecuteNonQuery()
    62.                         'trn.Commit()
    63.  
    64.  
    65.                         Response.Redirect("LogIn.aspx")
    66.                     Catch ex As OleDbException
    67.                         Response.Write("<http><body>" & vbNewLine)
    68.                         Response.Write("Error inserting: " & Now & "<br>" & vbNewLine)
    69.                         Response.Write(ex.Message.Replace(vbNewLine, "<br>") & "<br>" & vbNewLine)
    70.                         Response.Write(ex.ToString.Replace(vbNewLine, "<br>") & "<br>" & vbNewLine)
    71.                         Response.Write("<a href=""NewUser.aspx"">Click here to go back and try again</a>" & vbNewLine)
    72.                         Response.Write("</body></html>")
    73.                         Response.End()
    74.                     Finally
    75.                         If Not cmd_ins Is Nothing Then cmd_ins.Dispose()
    76.                     End Try
    77.                 End If
    78.             Catch ex As OleDbException
    79.                 Response.Write("<http><body>" & vbNewLine)
    80.                 Response.Write("Error:<br>" & vbNewLine)
    81.                 Response.Write(ex.Message.Replace(vbNewLine, "<br>") & "<br>" & vbNewLine)
    82.                 Response.Write("<a href=""NewUser.aspx"">Click here to go back and try again</a>" & vbNewLine)
    83.                 Response.Write("</body></html>")
    84.                 Response.End()
    85.             Finally
    86.                 If Not dr_chk Is Nothing Then dr_chk.Close()
    87.                 If Not cmd_chk Is Nothing Then cmd_chk.Dispose()
    88.             End Try
    89.         Else
    90.             Response.Write("<http><body>" & vbNewLine)
    91.             Response.Write("You MUST type a User Name and Password<br>" & vbNewLine)
    92.             Response.Write("<a href=""NewUser.aspx"">Click here to go back and try again</a>" & vbNewLine)
    93.             Response.Write("</body></html>")
    94.             Response.End()
    95.         End If
    96.     End Sub

  10. #10
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    the only other thing to check is your database fields, you might have one of them set to required and dont realize it. That is the only othe thing that may cause the error you reported, unless you are now getting a different error.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    ex.Message returns:
    Operation must use an updateable query.


    ex.ToString returns:
    System.Data.OleDb.OleDbException: Operation must use an updateable query.
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr)
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
    at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
    at dforums.NewUser.btnSubmit_Click(Object sender, EventArgs e) in C:\Inetpub\Public\dforums\NewUser.aspx.vb:line 97


    what's "Operation must use an updateable query." ?

  12. #12
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    "You need to set the folder and the database permissions
    so that ASPNET user has read, write."

    How do I do that ?

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    nevermind, I got it working.... the database was set to hidden attribute (i did that because I don't want anyone to download the database) but i guess I'll move it in a directory that is not shared on the web...

    Thanx for everything...

  15. #15
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Right click the mdb. Select Properties. goto the Security tab
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    I never thought that would be a problem, because i could add/modify data in the database from Access, so i thought it should work in my app also...

    I moved the DB to a directory where permissions are set to eveyone, and there is no need to make it hidden....

    Thanks again

  17. #17
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    "because i could add/modify data in the database from Access,"

    yeah but that would happen only under YOUR account, not the ASPNET account. But Everyone include ASP_NET, so that will be fine. But I would strongly suggest for security reasons, you lock it down to only allow you and ASPNET to access it.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width