mrnmnew
asked on
ASP .NET2 Updating SQL Record
I want to update a field in a record when the users selects that record. This is to record the number of 'clicks' a record gets. It sounds like it should be simple enough, but I am using ASP .NET2 and I am struggling. My code is:
Dim myval, mysql As String
myval = GridView1.SelectedDataKey. Value
mysql = "UPDATE supplier_details SET(clicks = (clicks + 1) WHERE (user_ID = '" + myval + "')"
EOLUpdate.UpdateCommand = mysql
EOLUpdate.Update()
but this returns an error 'In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user'
I have trawled the help pages and tried several versions of the code, but can not get things to work :(
Dim myval, mysql As String
myval = GridView1.SelectedDataKey.
mysql = "UPDATE supplier_details SET(clicks = (clicks + 1) WHERE (user_ID = '" + myval + "')"
EOLUpdate.UpdateCommand = mysql
EOLUpdate.Update()
but this returns an error 'In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user'
I have trawled the help pages and tried several versions of the code, but can not get things to work :(
ASKER
sorry that was me and my cut and pasting, the bracket is there in the real code.
Sorry about that
Sorry about that
Remove all the brackets and try it!
Seems to me, the error you are getting is in the line myval = GridView1.SelectedDataKey. Value
And if not, the error is from sql statement, as saleek said, you can remove the brackets soon after SET ( remove the bracket ) and remove the last bracket
If the error is in the line, myval = GridView1.SelectedDataKey. Value, can u just check by replacing the statement ( to verify the exact value in debug mode ) with Server.HtmlEncode(GridView 1.Selected DataKey.Va lue)
And if not, the error is from sql statement, as saleek said, you can remove the brackets soon after SET ( remove the bracket ) and remove the last bracket
If the error is in the line, myval = GridView1.SelectedDataKey.
ASKER
I have tried hard coding the sql as suggested but still get the error. when I single step the error occurs on the last line but that may not mean too much as that is the line that runs the query etc.
I would love to know what the error means, but can't find a description of it that makes any sense.
I would love to know what the error means, but can't find a description of it that makes any sense.
Are you trying to update a database or a DataTable in asp.net?
Make sure that [clicks] has no indexes on it.
Make sure that [clicks] has no indexes on it.
ASKER
EOLUpdate is a SQLDataSource object
The only index is one on the key field called line_number
The only index is one on the key field called line_number
Very strange problem.
Have you tried running the exact SQL that is generated in SQL Query Analyzer?
Does it work then?
I am also struggling to find anything on the internet... Have you used EOLUpdate somewhere else on this page to insert or update anything? Did it work?
What about changing your sql text into a stored procedure and just calling that through a SqlCommandObject?
Have you tried running the exact SQL that is generated in SQL Query Analyzer?
Does it work then?
I am also struggling to find anything on the internet... Have you used EOLUpdate somewhere else on this page to insert or update anything? Did it work?
What about changing your sql text into a stored procedure and just calling that through a SqlCommandObject?
ASKER
I have tried the sql in query analyer and it is fine.
EOLupdate connects fine and can return data from a select query, it seems it's the update that is causing problems.
I could try using a stored proc I guess, but I'm not convinced that is the problem. The only thing I found on the internet was a posting on a forum saying that .NET2 was very broken when connecting to datasources and quoted the same error message , but there was no answer :(
EOLupdate connects fine and can return data from a select query, it seems it's the update that is causing problems.
I could try using a stored proc I guess, but I'm not convinced that is the problem. The only thing I found on the internet was a posting on a forum saying that .NET2 was very broken when connecting to datasources and quoted the same error message , but there was no answer :(
Yeah I saw that posting as well.
I prefer Stored procedures for many reasons, the 2 biggest being security and speed.
I think it will work around the problem but I agree that is not the actual problem.
For the actual problem though since my brain has finally decided to join me :) (It's been a while)
What the SqlDataSource.Update() command does is execute the UpdateCommand setup INSIDE your SqlDataSource. The way you are trying to create it on the codebehind at the moment is incorrect.
The param's need to be specified seperately.
Take a look here
http://msdn2.microsoft.com/en-US/library/system.web.ui.webcontrols.sqldatasource.update(VS.80).aspx
If you want it to work in the codebehind try this
string mysql = "UPDATE supplier_details SET clicks = (clicks + 1) WHERE user_ID = @myVal"
EOLUpdate.UpdateCommand = mysql
EOLUpdate.UpdateParameters .Add("@myV al", GridView1.SelectedDataKey. Value)
EOLUpdate.Update()
That should work
I prefer Stored procedures for many reasons, the 2 biggest being security and speed.
I think it will work around the problem but I agree that is not the actual problem.
For the actual problem though since my brain has finally decided to join me :) (It's been a while)
What the SqlDataSource.Update() command does is execute the UpdateCommand setup INSIDE your SqlDataSource. The way you are trying to create it on the codebehind at the moment is incorrect.
The param's need to be specified seperately.
Take a look here
http://msdn2.microsoft.com/en-US/library/system.web.ui.webcontrols.sqldatasource.update(VS.80).aspx
If you want it to work in the codebehind try this
string mysql = "UPDATE supplier_details SET clicks = (clicks + 1) WHERE user_ID = @myVal"
EOLUpdate.UpdateCommand = mysql
EOLUpdate.UpdateParameters
EOLUpdate.Update()
That should work
ASKER
I'm gald your brain has joined you : )
I've just tried your suggestion and believe it or not I get the same error. :(
this is the full error:
System.Data.SqlClient.SqlE xception was unhandled by user code
Class=15
ErrorCode=-2146232060
LineNumber=1
Message="Line 1: Incorrect syntax near '('."
Number=170
Procedure=""
Server="TC"
Source=".Net SqlClient Data Provider"
State=1
StackTrace:
at System.Data.SqlClient.SqlC onnection. OnError(Sq lException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlI nternalCon nection.On Error(SqlE xception exception, Boolean breakConnection)
at System.Data.SqlClient.TdsP arser.Thro wException AndWarning (TdsParser StateObjec t stateObj)
at System.Data.SqlClient.TdsP arser.Run( RunBehavio r runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlC ommand.Fin ishExecute Reader(Sql DataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlC ommand.Run ExecuteRea derTds(Com mandBehavi or cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlC ommand.Run ExecuteRea der(Comman dBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlC ommand.Int ernalExecu teNonQuery (DbAsyncRe sult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlC ommand.Exe cuteNonQue ry()
at System.Web.UI.WebControls. SqlDataSou rceView.Ex ecuteDbCom mand(DbCom mand command, DataSourceOperation operation)
at System.Web.UI.WebControls. SqlDataSou rceView.Ex ecuteUpdat e(IDiction ary keys, IDictionary values, IDictionary oldValues)
at System.Web.UI.WebControls. SqlDataSou rceView.Up date(IDict ionary keys, IDictionary values, IDictionary oldValues)
at System.Web.UI.WebControls. SqlDataSou rce.Update ()
at _Default.GridView1_Selecte dIndexChan ged(Object sender, EventArgs e) in W:\EOLNET\Default.aspx.vb: line 47
at System.Web.UI.WebControls. GridView.O nSelectedI ndexChange d(EventArg s e)
at System.Web.UI.WebControls. GridView.H andleSelec t(Int32 rowIndex)
at System.Web.UI.WebControls. GridView.H andleEvent (EventArgs e, Boolean causesValidation, String validationGroup)
at System.Web.UI.WebControls. GridView.R aisePostBa ckEvent(St ring eventArgument)
at System.Web.UI.WebControls. GridView.S ystem.Web. UI.IPostBa ckEventHan dler.Raise PostBackEv ent(String eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData)
at System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt)
I've just tried your suggestion and believe it or not I get the same error. :(
this is the full error:
System.Data.SqlClient.SqlE
Class=15
ErrorCode=-2146232060
LineNumber=1
Message="Line 1: Incorrect syntax near '('."
Number=170
Procedure=""
Server="TC"
Source=".Net SqlClient Data Provider"
State=1
StackTrace:
at System.Data.SqlClient.SqlC
at System.Data.SqlClient.SqlI
at System.Data.SqlClient.TdsP
at System.Data.SqlClient.TdsP
at System.Data.SqlClient.SqlC
at System.Data.SqlClient.SqlC
at System.Data.SqlClient.SqlC
at System.Data.SqlClient.SqlC
at System.Data.SqlClient.SqlC
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at _Default.GridView1_Selecte
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.Process
Hmm wouldn't this be a different error?
This looks like a simple syntax error.
Let's check the SQL again.... try this
UPDATE supplier_details SET clicks = CONVERT(int, clicks) + 1 WHERE user_ID = @myVal
Could you post your new code as well if this fails.
This looks like a simple syntax error.
Let's check the SQL again.... try this
UPDATE supplier_details SET clicks = CONVERT(int, clicks) + 1 WHERE user_ID = @myVal
Could you post your new code as well if this fails.
ASKER
Dim myval, mysql As String
mysql = "UPDATE supplier_details SET clicks = CONVERT(int, clicks) + 1 WHERE user_ID = @myVal"
EOLUpdate.UpdateParameters .Add("@myV al", GridView1.SelectedDataKey. Value)
EOLUpdate.UpdateCommand = mysql
We are getting somewhere it seems, I get a different error :)
Must declare the variable '@myVal'.
when the codes runs. Not sure how to do this as dim myVal as string doesn't work nor does dim myVal as parameter or dim @myVal the @ is not allowed :(
mysql = "UPDATE supplier_details SET clicks = CONVERT(int, clicks) + 1 WHERE user_ID = @myVal"
EOLUpdate.UpdateParameters
EOLUpdate.UpdateCommand = mysql
We are getting somewhere it seems, I get a different error :)
Must declare the variable '@myVal'.
when the codes runs. Not sure how to do this as dim myVal as string doesn't work nor does dim myVal as parameter or dim @myVal the @ is not allowed :(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah it works !!!!
Final code is
Dim mysql As String
mysql = "UPDATE supplier_details SET clicks = CONVERT(int, clicks) + 1 WHERE user_ID = @myVal"
EOLUpdate.UpdateParameters .Add("myVa l", GridView1.SelectedDataKey. Value)
EOLUpdate.UpdateCommand = mysql
EOLUpdate.UpdateCommandTyp e = SqlDataSourceCommandType.T ext
EOLUpdate.Update()
Thanks for all your help.
The reason for the code being in the code behind page was that I wanted this code to run when the user opened up a details page on a particular record and so recorded the number of times the detail page was viewed so I put the code in the sub for _selectedIndexChanged event for the grid view.
I will assign max points to your answer, thanks for all the hard work, I wish I had a brain like yours
Mark Newton
Final code is
Dim mysql As String
mysql = "UPDATE supplier_details SET clicks = CONVERT(int, clicks) + 1 WHERE user_ID = @myVal"
EOLUpdate.UpdateParameters
EOLUpdate.UpdateCommand = mysql
EOLUpdate.UpdateCommandTyp
EOLUpdate.Update()
Thanks for all your help.
The reason for the code being in the code behind page was that I wanted this code to run when the user opened up a details page on a particular record and so recorded the number of times the detail page was viewed so I put the code in the sub for _selectedIndexChanged event for the grid view.
I will assign max points to your answer, thanks for all the hard work, I wish I had a brain like yours
Mark Newton
Hey I am just glad my brain finally decided to wake up :)
Glad to help...
Glad to help...
mysql = "UPDATE supplier_details SET(clicks = (clicks + 1) ) WHERE (user_ID = '" + myval + "')"