Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

SqlCommand Parameters.Add

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RN1 - 13 Mar 2008 00:26 GMT
Using SqlCommand, this is how I am updating a database table:

--------------------------------------------------------------------------------
Sub UpdateDataGrid(obj As Object, ea As DataGridCommandEventArgs)
   strSQL = "UPDATE Basket SET Quantity = Qty, Total = TotAmt WHERE
BasketID = BID AND ProductID = PID"

   sqlCmd = New SqlCommand(strSQL, sqlConn)
   With sqlCmd
       .Parameters.Add("Qty", SqlDbType.Int).Value = CInt(iQty)
       .Parameters.Add("TotAmt", SqlDbType.Money).Value = CInt(iQty)
* CType(ea.Item.FindControl("lblPrice"), Label).Text
       .Parameters.Add("BID", SqlDbType.VarChar, 50).Value =
strBasketID
       .Parameters.Add("PID", SqlDbType.VarChar, 50).Value =
CType(ea.Item.FindControl("lblID"), Label).Text
   End With

   sqlConn.Open()
   sqlCmd.ExecuteNonQuery()
   sqlConn.Close()
End Sub
--------------------------------------------------------------------------------

But the above code generates the following error pointing to the
sqlCmd.ExecuteNonQuery() line in the above code:

Invalid column name 'BID'.
Invalid column name 'PID'.

BID & PID are not the column names in the actual database table but
can't it be done in the way I have done above? In fact, Qty & TotAmt
are not the column names in the actual database table as well; so why
isn't the error pointing to Qty & TotAmt as they will be evaluated
before BID & PID, if I am not mistaken?

Thanks,

Ron
Scott M. - 13 Mar 2008 02:55 GMT
Parameter names must start with the "AT-Sign"

Parameters.Add("@BID", SqlDbType.VarChar, 50).Value = strBasketID
Parameters.Add("@PID", SqlDbType.VarChar, 50).Value =
CType(ea.Item.FindControl("lblID"), Label).Text

Without them there as markers, the CLR thinks the values you are looking for
are BID and PID.

-Scott

> Using SqlCommand, this is how I am updating a database table:
>
[quoted text clipped - 35 lines]
>
> Ron
Hans Kesting - 13 Mar 2008 10:59 GMT
Scott M. formulated on donderdag :
> Parameter names must start with the "AT-Sign"
>
[quoted text clipped - 6 lines]
>
> -Scott

Correct that the @-sign should be added to the parameter names (note:
also in the query), but I think it's SqlServer that is thinking "BID"
(without @) should refer to a column, not the .Net system.

Hans Kesting

>> Using SqlCommand, this is how I am updating a database table:
>>
[quoted text clipped - 35 lines]
>>
>> Ron
Peter Bromberg [C# MVP] - 13 Mar 2008 17:48 GMT
Sub UpdateDataGrid(obj As Object, ea As DataGridCommandEventArgs)
   strSQL = "UPDATE Basket SET Quantity = @Qty, Total = @TotAmt WHERE
BasketID = @BID AND ProductID = @PID"

   sqlCmd = New SqlCommand(strSQL, sqlConn)
   With sqlCmd
       .Parameters.Add("@Qty", SqlDbType.Int).Value = CInt(iQty)
       .Parameters.Add("@TotAmt", SqlDbType.Money).Value = CInt(iQty)
* CType(ea.Item.FindControl("lblPrice"), Label).Text
       .Parameters.Add("@BID", SqlDbType.VarChar, 50).Value =
strBasketID
       .Parameters.Add("@PID", SqlDbType.VarChar, 50).Value =
CType(ea.Item.FindControl("lblID"), Label).Text
   End With

   sqlConn.Open()
   sqlCmd.ExecuteNonQuery()
   sqlConn.Close()
End Sub

-- Cheers,Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net

> Using SqlCommand, this is how I am updating a database table:
>
[quoted text clipped - 35 lines]
>
> Ron
RN1 - 15 Mar 2008 12:09 GMT
On Mar 13, 9:48 pm, Peter Bromberg [C# MVP]
<pbromb...@yahoo.NoSpamMaam.com> wrote:
> Sub UpdateDataGrid(obj As Object, ea As DataGridCommandEventArgs)
>     strSQL = "UPDATE Basket SET Quantity = @Qty, Total = @TotAmt WHERE
[quoted text clipped - 62 lines]
>
> - Show quoted text -

Parameter names depend on the provider. When using the provider for
SQL Server, it should start with @ (e.g. @param1). For Oracle
provider, it should start with a colon (:)...for e.g. :param1. For
OleDb provider, just a question mark (?) would suffice

Ron
Scott M. - 15 Mar 2008 16:25 GMT
>Parameter names depend on the provider. When using the provider for
>SQL Server, it should start with @ (e.g. @param1). For Oracle
>provider, it should start with a colon (:)...for e.g. :param1. For
OleDb provider, just a question mark (?) would suffice

>Ron

Yes, that's what we've been telling you.  You are using SQL, so we talked
about "@".

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.