How can take the result of a Sql query and put it in a text field?
IIRC, not directly...as it's not text that's returned, it's Data. You would have to loop the data, appending it to your textbox. I have a feeling this isn't what you mean though...
What is it you are after though? Errors? Record Counts? Etc....
Well, just the data really. I'm trying to make a...well, I don't really know how to describe it... I'm trying to make my own form to edit data in a database: I can't seem to configure the GridView or the FormView exactly to my liking. I have two fields that correspond to columns in my table that I want to be able to edit; one is a title (varchar(50)) and the other is paragraph(s) of XML formatted text (varchar(MAX)). I have about 10 or 11 rows of data that I want to be able to edit, and I need to be able to have the textfields where the user enters in the data to be able to write the XML back to the database. (I need the XML since I'll have links inside the paragraphs that I'm entering). I can get GridView to list all the rows, but the edit fields are too limited (small) to edit paragraph effectively, and the FormView only lists one row at a time, which I also don't want (I don't want the user to have to page through each row until they find the one they want to edit).
I admit I'm a bit of a newbie at working with databases, and I've only just recently started learning ASP.NET, so my knowledge in how to tackle these problems are limited. Is this the best way to go about what I want to accomplish? Or is there a better way? Thanks for the feedback!!!
Here's how I managed to put a SQL result into TextBox1.
Basically, I'm counting the number of rows in a table called, tbl_Count. e.g. select count(id) as iCount from tbl_Count
Make sure you already have a connection string called, "MyConnectionString" in you web.config file and you can pretty much change the query to fit your needs.
// Make sure to import the following.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
// Start declaring the variables
string connString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
string strQuery = "select count(id) as iCount from tbl_Count";
// Run the query and dump the value into TextBox1
cmd.CommandText = strQuery;
cmd.Connection = conn;
conn.Open();
reader = cmd.ExecuteReader();
reader.Read();
TextBox1.Text = Convert.ToString(reader.GetValue(0));
// Close all connections
reader.Close();
conn.Close();
Hope it helps.
Yeah, that's exactly what I was looking for...sweet, thanks!
0 comments:
Post a Comment