Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - error @ ExecuteScalar() ? need help !

 
Thread Tools Search this Thread
Old 09-05-2005, 01:38 PM   #1
Default error @ ExecuteScalar() ? need help !


i am executing a query in DataGrid1_itemCOmmend.

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.
Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand

Dim intCount As Integer
Dim cmdvrfy As SqlCommand
cmdvrfy = New SqlCommand("Select * FROM tbl_rating Where DocumentID
=" & DocumentID & "AND ip =" & UserIP & ";", conPubs)
conPubs.Open()
cmdvrfy.Connection = conPubs
cmdvrfy.CommandType = CommandType.Text
intCount = cmdvrfy.ExecuteScalar()
conPubs.Close()
If intCount = 0 Then '127.0.0.1
'Opens the Panel for the user !
Dim MyPanel As Panel
MyPanel = (e.Item.FindControl("Panel1"))
MyPanel.Visible = True
Else
MsgBox("You have already rated this !!!!")
End If

End Sub

But i get this error msg. that is

Line 1: Incorrect syntax near '.0'.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect
syntax near '.0'.

Source Error:


Line 82: cmdvrfy.Connection = conPubs
Line 83: cmdvrfy.CommandType = CommandType.Text
Line 84: intCount = cmdvrfy.ExecuteScalar()
Line 85: conPubs.Close()
Line 86: If UserIP = "" Then '127.0.0.1


Source File: c:\inetpub\wwwroot\Rating\star_rating\WebForm1.asp x.vb

HELP me


jk velu
  Reply With Quote
Old 09-05-2005, 02:38 PM   #2
Hans Kesting
 
Posts: n/a
Default Re: error @ ExecuteScalar() ? need help !
jk velu wrote:
> i am executing a query in DataGrid1_itemCOmmend.
>
> Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
> System. Web.UI.WebControls.DataGridCommandEventArgs) Handles
> DataGrid1.ItemCommand
>
> Dim intCount As Integer
> Dim cmdvrfy As SqlCommand
> cmdvrfy = New SqlCommand("Select * FROM tbl_rating Where
> DocumentID =" & DocumentID & "AND ip =" & UserIP & ";", conPubs)


UserIP is a string, so you should enclose it in quotes
... "AND ip = ' " & UserIP & " ';" ... (note: extra spaces added for clarity - remove them)

By the way, it is better (safer, maybe even faster) to use parameters instead
of inline values.

Hans Kesting

> conPubs.Open()
> cmdvrfy.Connection = conPubs
> cmdvrfy.CommandType = CommandType.Text
> intCount = cmdvrfy.ExecuteScalar()
> conPubs.Close()
> If intCount = 0 Then '127.0.0.1
> 'Opens the Panel for the user !
> Dim MyPanel As Panel
> MyPanel = (e.Item.FindControl("Panel1"))
> MyPanel.Visible = True
> Else
> MsgBox("You have already rated this !!!!")
> End If
>
> End Sub
>
> But i get this error msg. that is
>
> Line 1: Incorrect syntax near '.0'.
> Description: An unhandled exception occurred during the execution of
> the current web request. Please review the stack trace for more
> information about the error and where it originated in the code.
>
> Exception Details: System.Data.SqlClient.SqlException: Line 1:
> Incorrect syntax near '.0'.
>
> Source Error:
>
>
> Line 82: cmdvrfy.Connection = conPubs
> Line 83: cmdvrfy.CommandType = CommandType.Text
> Line 84: intCount = cmdvrfy.ExecuteScalar()
> Line 85: conPubs.Close()
> Line 86: If UserIP = "" Then '127.0.0.1
>
>
> Source File: c:\inetpub\wwwroot\Rating\star_rating\WebForm1.asp x.vb
>
> HELP me





Hans Kesting
  Reply With Quote
Old 09-06-2005, 05:55 AM   #3
=?Utf-8?B?dmVsdQ==?=
 
Posts: n/a
Default Re: error @ ExecuteScalar() ? need help !
Hey Hans Kesting,

I was breaking my head for past 2 days exploring ExecuteScalar(). That was
really help full.
By da way, you said about something “use parameters”. I too read about it
but not sure about its implementation.

Would you mind sharing you thoughts on using parameters to this context I
stated below?

Dim UserIP As String
UserIP = (Request.UserHostAddress)
Dim intCount As Integer
Dim cmdvrfy As SqlCommand
cmdvrfy = New SqlCommand("Select * FROM tbl_rating Where DocumentID
=" & DocumentID & "AND ip ='" & UserIP & "';", conPubs)
conPubs.Open()
cmdvrfy.Connection = conPubs
cmdvrfy.CommandType = CommandType.Text
intCount = cmdvrfy.ExecuteScalar()
conPubs.Close()

thx
JK


"Hans Kesting" wrote:

> jk velu wrote:
> > i am executing a query in DataGrid1_itemCOmmend.
> >
> > Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
> > System. Web.UI.WebControls.DataGridCommandEventArgs) Handles
> > DataGrid1.ItemCommand
> >
> > Dim intCount As Integer
> > Dim cmdvrfy As SqlCommand
> > cmdvrfy = New SqlCommand("Select * FROM tbl_rating Where
> > DocumentID =" & DocumentID & "AND ip =" & UserIP & ";", conPubs)

>
> UserIP is a string, so you should enclose it in quotes
> ... "AND ip = ' " & UserIP & " ';" ... (note: extra spaces added for clarity - remove them)
>
> By the way, it is better (safer, maybe even faster) to use parameters instead
> of inline values.
>
> Hans Kesting
>
> > conPubs.Open()
> > cmdvrfy.Connection = conPubs
> > cmdvrfy.CommandType = CommandType.Text
> > intCount = cmdvrfy.ExecuteScalar()
> > conPubs.Close()
> > If intCount = 0 Then '127.0.0.1
> > 'Opens the Panel for the user !
> > Dim MyPanel As Panel
> > MyPanel = (e.Item.FindControl("Panel1"))
> > MyPanel.Visible = True
> > Else
> > MsgBox("You have already rated this !!!!")
> > End If
> >
> > End Sub
> >
> > But i get this error msg. that is
> >
> > Line 1: Incorrect syntax near '.0'.
> > Description: An unhandled exception occurred during the execution of
> > the current web request. Please review the stack trace for more
> > information about the error and where it originated in the code.
> >
> > Exception Details: System.Data.SqlClient.SqlException: Line 1:
> > Incorrect syntax near '.0'.
> >
> > Source Error:
> >
> >
> > Line 82: cmdvrfy.Connection = conPubs
> > Line 83: cmdvrfy.CommandType = CommandType.Text
> > Line 84: intCount = cmdvrfy.ExecuteScalar()
> > Line 85: conPubs.Close()
> > Line 86: If UserIP = "" Then '127.0.0.1


> > Source File: c:\inetpub\wwwroot\Rating\star_rating\WebForm1.asp x.vb
> >
> > HELP me




=?Utf-8?B?dmVsdQ==?=
  Reply With Quote
Old 09-06-2005, 09:04 AM   #4
Hans Kesting
 
Posts: n/a
Default Re: error @ ExecuteScalar() ? need help !

for an example, see here (watch for wrap):
http://msdn.microsoft.com/library/de...classtopic.asp
or here
http://www.dotnetjunkies.com/quickst...ess.aspx#param

Basically:
- write the query with named placeholders instead of literal values (note: no need for delimeters now)
- create parameters with correct type and value, and add them to the command object
- execute the query

you query would look like
Select * FROM tbl_rating Where DocumentID = @docid AND ip = @ip

advantage:
- better performance
- no problems with embedded quotes in strings or with dates
- not vulnerable to "sql injection" attacks

Hans Kesting


velu wrote:
> Hey Hans Kesting,
>
> I was breaking my head for past 2 days exploring ExecuteScalar().
> That was really help full.
> By da way, you said about something "use parameters". I too read
> about it but not sure about its implementation.
>
> Would you mind sharing you thoughts on using parameters to this
> context I stated below?
>
> Dim UserIP As String
> UserIP = (Request.UserHostAddress)
> Dim intCount As Integer
> Dim cmdvrfy As SqlCommand
> cmdvrfy = New SqlCommand("Select * FROM tbl_rating Where
> DocumentID =" & DocumentID & "AND ip ='" & UserIP & "';", conPubs)
> conPubs.Open()
> cmdvrfy.Connection = conPubs
> cmdvrfy.CommandType = CommandType.Text
> intCount = cmdvrfy.ExecuteScalar()
> conPubs.Close()
>
> thx
> JK
>
>
> "Hans Kesting" wrote:
>
>> jk velu wrote:
>>> i am executing a query in DataGrid1_itemCOmmend.
>>>
>>> Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
>>> System. Web.UI.WebControls.DataGridCommandEventArgs) Handles
>>> DataGrid1.ItemCommand
>>>
>>> Dim intCount As Integer
>>> Dim cmdvrfy As SqlCommand
>>> cmdvrfy = New SqlCommand("Select * FROM tbl_rating Where
>>> DocumentID =" & DocumentID & "AND ip =" & UserIP & ";", conPubs)

>>
>> UserIP is a string, so you should enclose it in quotes
>> ... "AND ip = ' " & UserIP & " ';" ... (note: extra spaces added for
>> clarity - remove them)
>>
>> By the way, it is better (safer, maybe even faster) to use
>> parameters instead
>> of inline values.
>>
>> Hans Kesting
>>
>>> conPubs.Open()
>>> cmdvrfy.Connection = conPubs
>>> cmdvrfy.CommandType = CommandType.Text
>>> intCount = cmdvrfy.ExecuteScalar()
>>> conPubs.Close()
>>> If intCount = 0 Then '127.0.0.1
>>> 'Opens the Panel for the user !
>>> Dim MyPanel As Panel
>>> MyPanel = (e.Item.FindControl("Panel1"))
>>> MyPanel.Visible = True
>>> Else
>>> MsgBox("You have already rated this !!!!")
>>> End If
>>>
>>> End Sub
>>>
>>> But i get this error msg. that is
>>>
>>> Line 1: Incorrect syntax near '.0'.
>>> Description: An unhandled exception occurred during the execution of
>>> the current web request. Please review the stack trace for more
>>> information about the error and where it originated in the code.
>>>
>>> Exception Details: System.Data.SqlClient.SqlException: Line 1:
>>> Incorrect syntax near '.0'.
>>>
>>> Source Error:
>>>
>>>
>>> Line 82: cmdvrfy.Connection = conPubs
>>> Line 83: cmdvrfy.CommandType = CommandType.Text
>>> Line 84: intCount = cmdvrfy.ExecuteScalar()
>>> Line 85: conPubs.Close()
>>> Line 86: If UserIP = "" Then '127.0.0.1

>
>>> Source File: c:\inetpub\wwwroot\Rating\star_rating\WebForm1.asp x.vb
>>>
>>> HELP me





Hans Kesting
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46