Results 1 to 11 of 11

Thread: VB.NET, MS Access, and parameters [RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92

    VB.NET, MS Access, and parameters [RESOLVED]

    This is a duplicate of a thread I started in the DB forum. After thinking about it, I thought I might get more help here as there are more eyes on it.

    So I'm going along, doing a fairly simple project for a software testing group that needs to communicate the defects they find. Basically the Access DB stores the defect number, date, and description. So I have :
    VB Code:
    1. Dim InsertData As String = "INSERT INTO Defects (DefectNumber,  DefectDescription, Date)" _
    2. & "VALUES(" & defectnum & ", '" & TextBox4.Text & "', '" & TextBox3.Text & "');"
    And it works great until someone used a double quote in the description (TextBox4.Text). So someone tells me to use parameters to get around that.

    I bought Microsoft ADO.NET Step by Step and didn't help at all. Most of it's examples were based using SQL Server, not Access, and I understand that there is some difference.

    Any advice on where I can get some helpful information?
    Last edited by shadowfyre; May 21st, 2004 at 01:19 PM.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I had this same problem. Single quotes would throw it off as well.
    I think I fixed it by using Replace() to strip out the quotes. Another option is to check for quotes in a key event and not allow them in the first place.
    If that doesn't help, I'll try looking it up at work tomorrow.

  3. #3
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124
    in the access, you create a queries first which is something like:


    INSERT INTO Job ( JobPO, JobCust, JobModel, JobSize, JobSubstance, JobColor, JobSet, JobSpeed, JobMin, JobOrder, JobPlanned, JobQty, JobReject, JobEff, JobMac, JobWorker, JobShift, JobDate, JobComments )
    VALUES ([@NewJobPO], [@NewJobCust], [@NewJobModel], [@NewJobSize], [@NewJobSubstance], [@NewJobColor], [@NewJobSet], [@NewJobSpeed], [@NewJobMin], [@NewJobOrder], [@NewJobPlanned], [@NewJobQty], [@NewJobReject], [@NewJobEff], [@NewJobMac], [@NewJobWorker], [@NewJobShift], [@NewJobDate], [@NewJobComments]);


    and in vb.net you have to pass in the parameters

    VB Code:
    1. Dim objCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("spInsertNewJob", OleDbConnection1)
    2.         objCmd.CommandType = CommandType.StoredProcedure
    3.  
    4.         Dim objParam As OleDb.OleDbParameter = New OleDb.OleDbParameter()
    5.         objParam = objCmd.Parameters.Add("@NewJobPO", OleDb.OleDbType.BSTR)
    6.         objParam.Direction = ParameterDirection.Input
    7.         objParam.Value = txtPONumber.Text
    8.  
    9.         objParam = objCmd.Parameters.Add("@NewJobCust", OleDb.OleDbType.BSTR)
    10.         objParam.Direction = ParameterDirection.Input
    11.         objParam.Value = ddlCustomer.SelectedItem.Text
    12.  
    13.         objParam = objCmd.Parameters.Add("@NewJobModel", OleDb.OleDbType.BSTR)
    14.         objParam.Direction = ParameterDirection.Input
    15.         objParam.Value = txtModel.Text
    16.  
    17.         objParam = objCmd.Parameters.Add("@NewJobSize", OleDb.OleDbType.BSTR)
    18.         objParam.Direction = ParameterDirection.Input
    19.         objParam.Value = txtSize.Text
    20.  
    21.         objParam = objCmd.Parameters.Add("@NewJobSubstance", OleDb.OleDbType.BSTR)
    22.         objParam.Direction = ParameterDirection.Input
    23.         objParam.Value = txtSubstance.Text
    24.  
    25.         objParam = objCmd.Parameters.Add("@NewJobColor", OleDb.OleDbType.BSTR)
    26.         objParam.Direction = ParameterDirection.Input
    27.         objParam.Value = ddlColor.SelectedItem.Text
    28.  
    29.         objParam = objCmd.Parameters.Add("@NewJobSet", OleDb.OleDbType.BSTR)
    30.         objParam.Direction = ParameterDirection.Input
    31.         objParam.Value = txtSettingTime.Text
    32.  
    33.         objParam = objCmd.Parameters.Add("@NewJobSpeed", OleDb.OleDbType.BSTR)
    34.         objParam.Direction = ParameterDirection.Input
    35.         objParam.Value = txtSpeed.Text
    36.  
    37.         objParam = objCmd.Parameters.Add("@NewJobMin", OleDb.OleDbType.BSTR)
    38.         objParam.Direction = ParameterDirection.Input
    39.         objParam.Value = txtPrintingTime.Text
    40.  
    41.         objParam = objCmd.Parameters.Add("@NewJobOrder", OleDb.OleDbType.BSTR)
    42.         objParam.Direction = ParameterDirection.Input
    43.         objParam.Value = txtOrderQuantity.Text
    44.  
    45.         objParam = objCmd.Parameters.Add("@NewJobPlanned", OleDb.OleDbType.BSTR)
    46.         objParam.Direction = ParameterDirection.Input
    47.         objParam.Value = txtPlannedQuantity.Text
    48.  
    49.         objParam = objCmd.Parameters.Add("@NewJobQty", OleDb.OleDbType.BSTR)
    50.         objParam.Direction = ParameterDirection.Input
    51.         objParam.Value = txtActualQuantity.Text
    52.  
    53.         objParam = objCmd.Parameters.Add("@NewJobReject", OleDb.OleDbType.BSTR)
    54.         objParam.Direction = ParameterDirection.Input
    55.         objParam.Value = txtRejectQuantity.Text
    56.  
    57.         objParam = objCmd.Parameters.Add("@NewJobEff", OleDb.OleDbType.BSTR)
    58.         objParam.Direction = ParameterDirection.Input
    59.         objParam.Value = txtEfficiency.Text
    60.  
    61.         objParam = objCmd.Parameters.Add("@NewJobMac", OleDb.OleDbType.BSTR)
    62.         objParam.Direction = ParameterDirection.Input
    63.         objParam.Value = ddlMachine.SelectedItem.Text
    64.  
    65.         objParam = objCmd.Parameters.Add("@NewJobWorker", OleDb.OleDbType.BSTR)
    66.         objParam.Direction = ParameterDirection.Input
    67.         objParam.Value = ddlWorker.SelectedItem.Text
    68.  
    69.         objParam = objCmd.Parameters.Add("@NewJobShift", OleDb.OleDbType.BSTR)
    70.         objParam.Direction = ParameterDirection.Input
    71.         objParam.Value = ddlShift.SelectedItem.Text
    72.  
    73.         objParam = objCmd.Parameters.Add("@NewJobDate", OleDb.OleDbType.BSTR)
    74.         objParam.Direction = ParameterDirection.Input
    75.         objParam.Value = txtDate.Text
    76.  
    77.         objParam = objCmd.Parameters.Add("@NewJobComments", OleDb.OleDbType.BSTR)
    78.         objParam.Direction = ParameterDirection.Input
    79.         objParam.Value = txtComment.Text

    Just let me know in case you don't understand.

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    But wouldn't there still be problems with quote marks in the textboxes?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    salvelinus, that's the whole point of using parameters. You can get double and single quotes into the DB with out the query bombing out. At least that is what I've read. I guess I'll find out.

    I've never really used Access before. I've figured out how to sorts and queries using that little table that lets you choose which columns you want to show and how you want them sorted, but I've never done an Insert in Access before. I've only done inserts and updates via VB as I've shown above.

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Well, maybe so, but I think Replace() would be less code.
    I don't use the Access Query Designer, I switch to SQL view to just write the SQL. It's easier to test SQL from your code that way.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    You're probably right about the Replace() being the easier way to do it, but there are some cons to doing it that way. First of all, I plan to display this information to other people. If someone needs to use a double or single qoute in the defect description, I would have to replace them when I put them into the DB with another couple characters, and then put them back in when I display the description at a later time. So that forces me to declare two other characters as unusable to my user base. Secondly, I guess the parameter way is the more "professional" way, and since this is for work, I should probably do it that way. Finally, since I don't know how to use parameters, this gives me a great oppurtunity to learn.

    Back to the subject at hand. I have the INSERT command in my database. How do I execute it while passing all these parameters?

  8. #8
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124
    You can see at here:

    VB Code:
    1. Dim objCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("spInsertNewJob", OleDbConnection1)
    2.         objCmd.CommandType = CommandType.StoredProcedure

    Above code is actually call the MS Access Queries name "spInsertNewJob"


    Now code below is actually passing all the neccessary parameters to the MS Access Queries.

    VB Code:
    1. Dim objParam As OleDb.OleDbParameter = New OleDb.OleDbParameter()
    2.         objParam = objCmd.Parameters.Add("@NewJobPO", OleDb.OleDbType.BSTR)
    3.         objParam.Direction = ParameterDirection.Input
    4.         objParam.Value = txtPONumber.Text
    5.  
    6.         objParam = objCmd.Parameters.Add("@NewJobCust", OleDb.OleDbType.BSTR)
    7.         objParam.Direction = ParameterDirection.Input
    8.         objParam.Value = ddlCustomer.SelectedItem.Text
    9.  
    10.         objParam = objCmd.Parameters.Add("@NewJobModel", OleDb.OleDbType.BSTR)
    11.         objParam.Direction = ParameterDirection.Input
    12.         objParam.Value = txtModel.Text
    13.  
    14. ...

    hope that answered your question. I found this from a book which is "SAM Teach yourself ASP.NET In 21 Days", examples of asp.net pages' code behind are mainly vb.net. Moreoever, this book shows you how to handle the "Transaction". Few Sql statement combine to become a TRANSACTION. If any one of the statement goes wrong, the Data will be rolled back to the initial stage.

  9. #9
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124
    sorrie, for execution part is

    VB Code:
    1. Try
    2.             OleDbConnection1.Open()
    3.             objCmd.ExecuteNonQuery()
    4.    
    5.             Me.ClearAllEntry()
    6.         Catch ex As OleDb.OleDbException
    7.             Response.Write(ex.Message)
    8.         Finally
    9.             OleDbConnection1.Close()
    10.         End Try
    11.     End Sub

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92

    VB.NET, MS Access, and parameters [RESOLVED]

    Beautiful. I think I've got it now. Just for completeness sake here is the relavent chunck of my code

    VB Code:
    1. Dim sConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    2.                                     "Data Source = C:\old\DBTEST\PatchMan_test.mdb;"
    3.         Dim oOleDbConnection As New OleDb.OleDbConnection(sConnString)
    4.         'Dim InsertData As String = "INSERT INTO Defects (DefectNumber, DefectVersion, DefectArea, DefectDescription, RbApprovedDate, UserRb)" _
    5.         '                        & "VALUES(" & defectnum & ", '" & VersionNum & "', '" & Component & "', '" & TextBox4.Text & "', '" & TextBox3.Text & "', '" & TextBox2.Text & "');"
    6.  
    7.         Dim myOleCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand("AddDefect", oOleDbConnection)
    8.         myOleCommand.CommandType = CommandType.StoredProcedure
    9.         Dim objParam As OleDb.OleDbParameter = New OleDb.OleDbParameter
    10.  
    11.         objParam = myOleCommand.Parameters.Add("@DefectNumber", OleDb.OleDbType.BSTR)
    12.         objParam.Direction = ParameterDirection.Input
    13.         objParam.Value = defectnum
    14.  
    15.         objParam = myOleCommand.Parameters.Add("@DefectArea", OleDb.OleDbType.BSTR)
    16.         objParam.Direction = ParameterDirection.Input
    17.         objParam.Value = Component
    18.  
    19.         objParam = myOleCommand.Parameters.Add("@DefectVersion", OleDb.OleDbType.BSTR)
    20.         objParam.Direction = ParameterDirection.Input
    21.         objParam.Value = VersionNum
    22.  
    23.         objParam = myOleCommand.Parameters.Add("@DefectDescription", OleDb.OleDbType.BSTR)
    24.         objParam.Direction = ParameterDirection.Input
    25.         objParam.Value = TextBox4.Text
    26.  
    27.         objParam = myOleCommand.Parameters.Add("@RbApprovedDate", OleDb.OleDbType.BSTR)
    28.         objParam.Direction = ParameterDirection.Input
    29.         objParam.Value = TextBox3.Text
    30.  
    31.         objParam = myOleCommand.Parameters.Add("@UserRb", OleDb.OleDbType.BSTR)
    32.         objParam.Direction = ParameterDirection.Input
    33.         objParam.Value = TextBox2.Text
    34.  
    35.         Try
    36.             oOleDbConnection.Open()
    37.             MessageBox.Show("connection open", "connection open")
    38.             myOleCommand.ExecuteNonQuery()
    39.             MessageBox.Show("execute insert", "execute insert")
    40.             MessageBox.Show("Defect " & TextBox1.Text & " has been added.", "Success")
    41.         Catch ex As Exception
    42.             MessageBox.Show(ex.Message, "Caught Exception")
    43.         Finally
    44.             oOleDbConnection.Close()
    45.         End Try


    Thanks chinhow.

    salvelinus, just for future reference, it does preserve double and single quotes.

  11. #11
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124
    You are welcome, It's my pleasure

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