473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

getting an asp:textbox value to an sql parameter

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
4 6444
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
<<< 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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alex | last post by:
I am a newbie to ASP.NET... What I was trying to do... but yet to succeed in... I am building a customer info database... and on default, I want to display the customer's basic information in...
2
by: Kerri | last post by:
Hi, I have an asp textbox that allows users to enter their password. I have another page that allows a user to edit their user account. On this page I set the text to be the password in...
4
by: Chumley the Walrus | last post by:
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"...
7
by: Jan Hoffman | last post by:
hi @all I have the following element in an ascx-file: <asp:TextBox id="name" runat="server">name</asp:TextBox> The textbox belongs to an application of a telefonebook. I want that if I...
3
by: Dave | last post by:
I have a button that calls a JavaScript. I need to get the value in an asp:TextBox field to the javascript, either as a parameter or via some mechanism within the JavaScript itself. Any Ideas? ...
1
by: Shilpi Chaudhry | last post by:
I have a forloop which I use to create textboxes - but unfortunately all of them have the same ids - I need to assign different ids to these textboxes creeated in the loop so that I can retrieve...
3
by: Patrick | last post by:
Try to put in a TextBox next month's date in format (MMM yyyy). The following does NOT work, why is that? <!--When rendered, the texbox is blank, although the text in the round bracket after the...
0
by: datakix | last post by:
After 16 hours of frustration, I've managed to solve this problem for a project I'm working on. The 'trick' is set EnableViewState="False" for the asp:textbox inside the Repeater control. The...
11
by: Joe | last post by:
Hello All, I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton (SearchButton) server controls. The user can type search text in SearchTextBox and click SearchButton and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.