getting an asp:textbox value to an sql parameter

  • Thread starter Thread starter Chumley the Walrus
  • Start date Start date
C

Chumley the Walrus

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
 
<<< 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
 
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
 
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
 
Back
Top