473,396 Members | 1,760 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,396 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
Nov 18 '05 #1
4 1356
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

Nov 18 '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

Nov 18 '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
Nov 18 '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
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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"...
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: Matthew Wieder | last post by:
Hi - I'm trying to do client-side validation of the text in a Asp.Net textbox control using javascript. My page has the following: <form id="Form1" method="post" runat="server" onsubmit="return...
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...
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
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.