Showing posts with label modify. Show all posts
Showing posts with label modify. Show all posts

Monday, March 26, 2012

Help with assigning variables to from a SQL query

I've reconfigured Microsoft's IBS Store shopping cart to function within a small e-commerce website. What I am trying to do is to modify the code slightly in order to use a third party credit card processing center. The situation is this: once the customer clicks the final "check out" button, a stored procedure writes all of the product ordering information into the database. I, then, capture what they're wanting to purchase with the following SQL statement:

Dim strSQL as String = "Select orderID, modelNumber from orderDetails" & _
"where CustomerID = " & User.Identity.Name & _
"And orderid = (SELECT MAX(orderid)FROM orderDetails" & _
"where CustomerID = " & User.Identity.Name & ")"

What I would like to do is assign specific values to variables based off of the above query. For example:

Dim orderItem as String = (all of the modelNumbers from the query)
Dim orderIdItem as String = (all of the orderIDs from the query)

How do I do this?? Any help is much appreciated! Thanks in advance.

RonI'm not fluent in VB, but try to grasp the outline of the code :)

[code]
Dim myConnection As SqlConnection = New SqlConnection("..my connection string..")
Dim myCommand As SqlCommand = New SqlCommand("the query...")
myCommand.Connection = myConnection
myConnection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader()

'String Builder objects speed up performance, as strings are immutable.
Dim modelNumbers As System.Text.StringBuilder = New System.Text.StringBuilder
Dim orderID As System.Text.StringBuilder = New System.Text.StringBuilder

' Go through all the rows of the query
While (myReader.Read())
modelNumbers.Append(myReader.GetString(0)) 'Add the ModelNumber of the row to the string
orderID.Append(myReader.GetString(1)) ' Add the OrderID of the row to the string
End While

myReader.Close()
myConnection.Close()
[/code]

HTH|||Thanks for your help.

The code creates this error: "Specified cast is not valid", from the following line:

modelNumbers.Append(myReader.GetString(0)) 'Add the ModelNumber of the row to the string

Any ideas?

Again, thanks for your help!

Ron|||The field is either null or its a byte field or something.

I would check if its null first before adding it to the string.

HTH
Tony|||It doesn't come back null. To test this I simply bound the query data to a datagrid in order to display the information on the page. There are no null items; only data...as expected|||Try


modelNumbers.Append(myReader(0).ToString())

Does this help|||This worked beautifully.
Thank you!

Sunday, February 19, 2012

Help Req with Locking Tables...

Hi DB Gurus..

I need to lock one table ( table-level locking) as i dont want anyone to modify till i finish my stored procedure.

I need simple example on how to Lock the table and unlock..

Something like this

Lock Table Name...
...

statements go here
...

Unlock table...

Is this possible in Sql server 2000

waiting for ur replies..

Thanks in AdvanceI dont know whether this is efficient method

begin transaction

...

statements go here
...

Commit transaction|||Hey Guys..

anymore suggestions...|||Whenever I see anyone suggest locking a table, the first thing that comes to my mind is to stop SQL Server, then restart it in single user mode. While some folks consider this a bit drastic, it has about the same effect on scalability, it is far more efficient, and it is less uncertain.

-PatP