Bind data to TextBox

G

G

Hello,

Fairly new to .net (using c#).

I am running an SQL query which always returns ONE ROW and no more. I would
like to have the ID field of the returned row bound to a TextBox onSubmit.

AT the moment I have used the following controls:

<asp:SqlDataSource ID="DStypes" runat="server" />
<asp:GridView ID="GVtypes" runat="server"
DataSourceID="DStypes">
</asp:GridView>

This returns the entire row, I was using this to test the data returned was
correct. I would like to amend this code if possible to no longer return
the datagrid, but simply the value of the ID field, and stick it inside my
TextBox.

Should I be using SqlDataSource to achieve this?

Regards,

Gary.
 
E

Eliyahu Goldin

Gary,

Make a SqlCommand object for the query that will return only one field, then
call its ExecuteScalar method. This will return the ID value. Once you have
the value, you can convert it to string with the ToString method and assign
to your textbox's Text property.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top