By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,967 Members | 1,596 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,967 IT Pros & Developers. It's quick & easy.

getting an asp:textbox value to an sql parameter

P: n/a
I'm using this sql parameter:

MyCommand.Parameters("@Sport").value = Server.HtmlEncode(sport)

to get a value from a textbox control:

<asp:TextBox Name="sport" id="sport" runat="server"></asp:TextBox>

but i cannot see why I get "Value of type
'System.Web.UI.WebControls.TextBox' cannot be converted to 'String'"
error when the value I'm passing is a string.

???
Chumley
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Try:

MyCommand.Parameters("@Sport").value = Server.HtmlEncode(sport.Text)
Chumley the Walrus wrote:
I'm using this sql parameter:

MyCommand.Parameters("@Sport").value = Server.HtmlEncode(sport)

to get a value from a textbox control:

<asp:TextBox Name="sport" id="sport" runat="server"></asp:TextBox>

but i cannot see why I get "Value of type
'System.Web.UI.WebControls.TextBox' cannot be converted to 'String'"
error when the value I'm passing is a string.

???
Chumley

Jul 21 '05 #2

P: n/a
<<< but i cannot see why I get "Value of type
'System.Web.UI.WebControls.TextBox' cannot be converted to 'String'" error
when the value I'm passing is a string >>>
The reason you get this error is because in the .NET world, objects like
TextBoxes do not have default property values. In the VB6 world, textboxes
had a *default property* (which was text)... meaning that your syntax would
have worked... i.e., you simply specify the object (sport - which is a
textbox), and because you didn't specify the text property (sport.text), the
default property would be retrieved automatically for you. In other words,
VB6 allowed you to get away without specifying the .text property. .NET does
not allow for default properties... you must specify the .text property
explicitly (sport.Text)... and if you're using C#, be sure to get the case
right (sport.text would be different than sport.Text).

HTH

"Chumley the Walrus" <sp*******@yahoo.com> wrote in message
news:1e**************************@posting.google.c om...
I'm using this sql parameter:

MyCommand.Parameters("@Sport").value = Server.HtmlEncode(sport)

to get a value from a textbox control:

<asp:TextBox Name="sport" id="sport" runat="server"></asp:TextBox>

but i cannot see why I get "Value of type
'System.Web.UI.WebControls.TextBox' cannot be converted to 'String'"
error when the value I'm passing is a string.

???
Chumley

Jul 21 '05 #3

P: n/a
Hi Chumley,

Probably
MyCommand.Parameters("@Sport").value = sport.text

(When you have added the parameter before to MyCommand, however I saw that
in another message. It is better to stay in the same thread by the way, than
people can see what you have done already)

I hope this helps?

Cor
Jul 21 '05 #4

P: n/a
> Probably
MyCommand.Parameters("@Sport").value = sport.text


That again gives me an Object reference not set to an instance of an
object error.
Here is the subroutine I'm using with the click event and such (this
is part of a datagrid display, as for the BindGrid() reference). I'm
able to view the page that displays the datagrid and textboxes/button,
but get the error when i try to add something to the 'sport' and
'articleheader' textboxes:

Sub AddArticle_Click(Sender As Object, E As EventArgs)
If (Page.IsValid)

Dim DS As DataSet
Dim MyCommand As SqlCommand

Dim InsertCmd As String = "insert into tblArticles
(sport,articleheader) values (@Sport, @articleheader)"

MyCommand.Parameters.Add(New SqlParameter("@Sport",
SqlDbType.VarChar, 50))
MyCommand.Parameters("@Sport").value = sport.Text

MyCommand.Parameters.Add(New SqlParameter("@articleheader",
SqlDbType.nVarChar, 255))
MyCommand.Parameters("@articleheader").value =
articleheader.Text

MyCommand.Connection.Open()

' testing from aspalliance stuff
myCommand.ExecuteNonQuery()

MyCommand.Connection.Close()

End If

BindGrid()
End Sub
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.