(asp.net) my sql-code (update statement) works in database but not in program - asp.net

I wrote a program to easily store/edit my comic book collection. I can add, delete and request anything (comic/character/team) by my update statement doesn't work. I made a copy in a query in my access database and that worked perfectly. The strangest thing is that my program doesn't give any errors, it completes the transaction but doesn't change the data in my database. (this is a problem on both comic, character and team updates).
Here is my code to update a comic.
Public Sub UpdateComic(comic As Comic)
Dim cn As New OleDbConnection(persistenceController.connectiestring)
Dim cmd As New OleDbCommand("UPDATE tblComics SET serie = #serie, volume = #volume, issue = #issue, release = #release, inpossession = #inpossession, timesread = #read, story = #story, Languages = #languages WHERE ID = #ID", cn)
cmd.Parameters.AddWithValue("serie", comic.Serie)
cmd.Parameters.AddWithValue("volume", comic.Volume)
cmd.Parameters.AddWithValue("issue", comic.Issue)
cmd.Parameters.AddWithValue("release", comic.Release)
cmd.Parameters.AddWithValue("inpossession", comic.InPossession)
cmd.Parameters.AddWithValue("read", comic.Read)
cmd.Parameters.AddWithValue("story", comic.story)
cmd.Parameters.AddWithValue("ID", comic.ID)
cmd.Parameters.AddWithValue("Languages", comic.Language)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
End Sub
Can anyone help me with this.
Thanks in advance.

Switch the position of the last two parameters added to the OleDbParameterCollection
cmd.Parameters.AddWithValue("Languages", comic.Language)
cmd.Parameters.AddWithValue("ID", comic.ID)
OleDb doesn't recognize the parameters by their name, but by their position. You should add the parameters in the exact order defined by the parameter placeholders in the command text
By the way. OleDb wants its placeholders parameter defined with the single question mark character (?), but MS-Access accepts also the syntax #name probably to have a better upgrade compatibility with its big cousin Sql Server.

Related

converting MSIDXS search to Windows Search Service for website

Here's my situation: I'm in the process of a website overhaul to the public website for our company. The old site was running on server 2003 on .net framework 3.5? it may have been originally built on the 2.0 framework for that matter. ANYway, the old site had a search feature which worked really nicely for users to find pages related to topics they were interested in. It used the old MSIDXS oledb connection type... simple code as follows...
Dim odbSearch As New System.Data.OleDb.OleDbConnection("Provider=""MSIDXS"";Data Source=""Proto"";")
Dim cmdSearch As New System.Data.OleDb.OleDbCommand
cmdSearch.CommandText = String.Format("SELECT doctitle, filename, vpath, rank, characterization, size FROM scope() WHERE FREETEXT(Contents, '{0}') ORDER BY rank DESC", searchText) WHERE CONTAINS(*,'\""" & searchText & "*\""') AND scope='file:C:\...\Web_App' ORDER BY System.ItemPathDisplay DESC
This worked great. But now we're moving this to a 2008 r2 server, which doesn't have the MSIDXS indexing anymore... or it does, but doesn't work for sites? I was able to turn it on, but it never found anything, and the catalog remained empty, and everything I've read said this isn't how to do searches on sites anymore. The 'new way' that I read about was using windows search service. I've adjusted the service on the box to 'index' the website's directory, and it seems to have stuff in the catalog... however, what code I've converted always returns 0. so the new code looks like...
Dim odbSearch As New System.Data.OleDb.OleDbConnection("Provider=Search.CollatorDSO.1;Extended Properties='Application=Windows';")
cmdSearch.CommandText = String.Format("SELECT system.title, system.filename, System.ItemPathDisplay FROM SystemIndex WHERE scope='file:C:\...\Web_App'")
Dim rdrSearch As OleDbDataReader = cmdSearch.ExecuteReader()
While rdsSearch.read()
I can't get this to actually return any results. regardless of what I put in as the search criteria, it jumps right to the end while.
Can someone tell me what piece of the puzzle I'm missing?
Everything was actually correct; I wasn't getting any rows back because I had already narrowed the systemIndex to be looking at the subdirectory that I wanted, so including a scope in the where clause was causing zero results. As soon as I took that out, the search works.
It looks that the scope should be specified as 'file:C:\\...\\Web_App', or add a '#' sign at the beginning of your query string.

Change ODBC-Connection in Access 2007

I hava a Pass-Through sql-query in access which queries my mysql-db.
My current ODBC connection for the query is defined as follows:
ODBC;UID=access_frontend; PWD=hello#world; DSN=my_db_test;
If I change my ODBC connection from my test to my normal DB
ODBC;UID=access_frontend; PWD=hello#world; DSN=my_db;
If I save my changes and restart again, Access has changed it back to my_db_test.
Is there any place where I can globally change my ODBC connection?`
I do not get this problem changing in code or manually. You can change the connection through VBA:
Dim qdf As QueryDef
''dbQSQLPassThrough = 112
For Each qdf In CurrentDb.QueryDefs
If qdf.Type = dbQSQLPassThrough Then
Debug.Print qdf.connect
qdf.connect = "ODBC;filedsn=z:\docs\test.dsn;"
Debug.Print qdf.connect
End If
Next
You will notice that the passthrough query illustrated refers to:
filedsn=z:\docs\test.dsn;
This is another easy way of changing the connection, just change the DSN, in the above case, you could just edit the file test.dsn.

Link DB in Oracle parameter problem

I've got this Oracle query that links to another db but it doesn't seem to accept my parameters that i pass to the query
Here's my code
cmd.CommandText = "SELECT * FROM table1#dev tb1 join table2 tb2 on tb1.id = tb2.id WHERE tb1.id = :id"
cmd.CommandType = CommandType.Text
cmd.BindByName = True
cmd.Parameters.Add(New OracleParameter("id", id))
I get this error when do this
ORA-03113: end-of-file on communication channel
But if i simply change the query to not be using parameters then it works
Any ideas on how I could get this working with parameters?
EDIT
On further investigation if I try the following
cmd.CommandText = "SELECT * FROM table1#dev tb1 WHERE tb1.id = :id"
cmd.CommandType = CommandType.Text
cmd.BindByName = True
cmd.Parameters.Add(New OracleParameter("id", id))
I get a different error
ORA-28511: lost RPC connection to heterogeneous remote agent
I would really like a solution which allows parameters to be passed to the linked db but I can't seem to find anything about using parameters with linked db's
Thanks
ORA-03113 is a generic exception, hurled when something catastrophic causes the connection to drop. There may be some additional information in the alert log or a trace file. Check the background dump or diagnostics directory.
ORA-28511 is rather more specific. The error message points to a particular area for investigation; the advice is "Check for network problems and remote host crashes. The problem is probably in the agent software. "
So, it's not really a syntax problem. It's an environment (configuration/hardware) issue. I'm afraid you're going to have to do a some more digging.
Good luck.
have you tried specifying the parameter type ?:
OracleParameter p1 = new OracleParameter("id",OracleDbType.Decimal);
p1.Value=id;
cmd.Parameters.Add(p1);
Personally, this whole Oracle driver/query stuff really made me go nuts; e.g. I experienced that certain SELECT * generated a DivideByZeroException while replacing the * by the really required column names worked like a charm.
It could be that you have a similar issue here.
What helped me resolving the issues was to choose the ODP.NET drivers really wisely; we are running a production system here with beta drivers since they were the only ones working in all circumstances in our project.

ASP shopping cart

Hello based on the example at http://www.15seconds.com/issue/010411.htm i will like to create an ASP shop but i would like to rewrite the connection to db as i don't have a dedicated server .
Instead of
Sub SubGetDSNCreateConn
strBasketDSN = Application("strBaskDSN")
set BaskConn = server.CreateObject ("ADODB.Connection")
BaskConn.ConnectionString = strBasketDSN
BaskConn.Open
End Sub
and
set SavedBaskConn = server.CreateObject ("adodb.connection")
SavedBaskConn.ConnectionString = Application("strBaskDSN")
SavedBaskConn.Open
i would like to use something like:
conn="provider=sqloledb;server=localhost;uid=username;pwd=password;database=shop"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strsql, conn
and i can't make it work .. does anybody could point me in the right direction or give me a better tutorial how to create a classic ASP shopping cart that could hold a big traffic?
Thank you
can i ask why you don't just change the settings in:
strBasketDSN = Application("strBaskDSN")
to match your requirement, rather than changing the code entirely?? I would be tempted to examine the contents of Application("strBaskDSN") and just change those to match your own database.
If i'm missing something obvious, then let me know.
jim

What's wrong with this ASP recursive function?

When I call this function, everything works, as long as I don't try to recursively call the function again. In other words if I uncomment the line:
GetChilds rsData("AcctID"), intLevel + 1
Then the function breaks.
<%
Function GetChilds(ParentID, intLevel)
Set rsData= Server.CreateObject("ADODB.Recordset")
sSQL = "SELECT AcctID, ParentID FROM Accounts WHERE ParentID='" & ParentID &"'"
rsData.Open sSQL, conDB, adOpenKeyset, adLockOptimistic
If IsRSEmpty(rsData) Then
Response.Write("Empty")
Else
Do Until rsData.EOF
Response.Write rsData("AcctID") & "<br />"
'GetChilds rsData("AcctID"), intLevel + 1
rsData.MoveNext
Loop
End If
rsData.close: set rsData = nothing
End Function
Call GetChilds(1,0)
%>
*Edited after feedback
Thanks everyone,
Other than the usual error:
Error Type: (0x80020009) Exception occurred.
I wasn't sure what was causing the problems. I understand that is probably due to a couple of factors.
Not closing the connection and attempting to re-open the same connection.
To many concurrent connections to the database.
The database content is as follows:
AcctID | ParentID
1 Null
2 1
3 1
4 2
5 2
6 3
7 4
The idea is so that I can have a Master Account with Child Accounts, and those Child Accounts can have Child Accounts of their Own. Eventually there will be Another Master Account with a ParentID of Null that will have childs of its own. With that in mind, am I going about this the correct way?
Thanks for the quick responses.
Thanks everyone,
Other than the usual error:
Error Type: (0x80020009) Exception
occurred.
I wasn't sure what was causing the problems. I understand that is probably due to a couple of factors.
Not closing the connection and attempting to re-open the same connection.
To many concurrent connections to the database.
The database content is as follows:
AcctID | ParentID
1 Null
2 1
3 1
4 2
5 2
6 3
7 4
The idea is so that I can have a Master Account with Child Accounts, and those Child Accounts can have Child Accounts of their Own. Eventually there will be Another Master Account with a ParentID of Null that will have childs of its own. With that in mind, am I going about this the correct way?
Thanks for the quick responses.
Look like it fails because your connection is still busy serving the RecordSet from the previous call.
One option is to use a fresh connection for each call. The danger there is that you'll quickly run out of connections if you recurse too many times.
Another option is to read the contents of each RecordSet into a disconnected collection: (Dictionary, Array, etc) so you can close the connection right away. Then iterate over the disconnected collection.
If you're using SQL Server 2005 or later there's an even better option. You can use a CTE (common table expression) to write a recursive sql query. Then you can move everything to the database and you only need to execute one query.
Some other notes:
ID fields are normally ints, so you shouldn't encase them in ' characters in the sql string.
Finally, this code is probably okay because I doubt the user is allowed to input an id number directly. However, the dynamic sql technique used is very dangerous and should generally be avoided. Use query parameters instead to prevent sql injection.
I'm not too worried about not using intLevel for anything. Looking at the code this is obviously an early version, and intLevel can be used later to determine something like indentation or the class name used when styling an element.
Running out of SQL Connections?
You are dealing with so many layers there (Response.Write for the client, the ASP for the server, and the database) that its not surprising that there are problems.
Perhaps you can post some details about the error?
hard to tell without more description of how it breaks, but you are not using intLevel for anything.
How does it break?
My guess is that after a certain number of recursions you're probably getting a Stack Overflow (ironic) because you're not allocating too many RecordSets.
In each call you open a new connection to the database and you don't close it before opening a new one.
Not that this is actually a solution to the recursion issue, but it might be better for you to work out an SQL statement that returns all the information in a hierarchical format, rather than making recursive calls to your database.
Come to think of it though, it may be because you have too many concurrent db connections. You continually open, but aren't going to start closing until your pulling out of your recursive loop.
try declaring the variables as local using a DIM statement within the function definition:
Function GetChilds(ParentID, intLevel)
Dim rsData, sSQL
Set ...
Edit: Ok, I try to be more explicit.
My understanding is that since rsData is not declared by DIM, it is not a local variable, but a global var. Therefore, if you loop through the WHILE statement, you reach the .Eof of the inner-most rsData recordset. You return from the recursive function call, and the next step is again a rsData.MoveNext, which fails.
Please correct me if rsData is indeed local.
If you need recursion such as this I would personally put the recursion into a stored procedure and handle that processing on the database side in order to avoid opening multiple connections. If you are using mssql2005 look into something called Common Table Expressions (CTE), they make recursion easy. There are other ways to implement recursion with other RDBMS's.
Based on the sugestions I will atempt to move the query into a CTE (common table expression) when I find a good tutorial on how to do that. For now and as a quick and dirty fix, I have changed the code as follows:
Function GetChilds(ParentID, intLevel)
'Open my Database Connection and Query the current Parent ID
Set rsData= Server.CreateObject("ADODB.Recordset")
sSQL = "SELECT AcctID, ParentID FROM Accounts WHERE ParentID='" & ParentID &"'"
rsData.Open sSQL, conDB, adOpenKeyset, adLockOptimistic
'If the Record Set is not empty continue
If Not IsRSEmpty(rsData) Then
Dim myAccts()
ReDim myAccts(rsData.RecordCount)
Dim i
i = 0
Do Until rsData.EOF
Response.Write "Account ID: " & rsData("AcctID") & " ParentID: " & rsData("ParentID") & "<br />"
'Add the Childs of the current Parent ID to an array.
myAccts(i) = rsData("AcctID")
i = i + 1
rsData.MoveNext
Loop
'Close the SQL connection and get it ready for reopen. (I know not the best way but hey I am just learning this stuff)
rsData.close: set rsData = nothing
'For each Child found in the previous query, now lets get their childs.
For i = 0 To UBound(myAccts)
Call GetChilds(myAccts(i), intLevel + 1)
Next
End If
End Function
Call GetChilds(1,0)
I have working code with the same scenario.
I use a clientside cursor
...
rsData.CursorLocation = adUseClient
rsData.Open sSQL, conDB, adOpenKeyset, adLockOptimistic
rsData.ActiveConnectcion = Nothing
...
as pointed out in other responses, this is not very efficient, I use it only in an admin interface where the code is called infrequently and speed is not as critical.
I would not use such a recursive process in a regular web page.
Either rework the code to get all data in one call from the database, or make the call once and save it to a local array and save the array in an application variable.

Resources