954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# TextBoxes

I am trying to display a database into textboxes without using Datagrids. How do you give a value to a text box that corresponds with a Sql row.

Thanks.

Munoz
Newbie Poster
7 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

What I like to do:

1. Perform the query
2. Use a SqlDataReader for a forward-only journey through the results
3. Simply assign the values from the Reader to the appropriate controls:

myTextBox.Text = myDataReader["fieldName"].ToString();

There are other ways, involving DataBinding some ADO.NET stuff to your controls - I've never bothered with them. I like my code to be self-documenting and very straightforward, and never had any issues with a simple query or storedproc to get the data, and then do whatever I want with it.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

This is what I have. What's wrong with this? (DataReader). Thanks!!

string fName=Request.QueryString["id"];
string connStr = (ConfigurationSettings.AppSettings["dsn_SQL"]);
SqlConnection conn = new SqlConnection(connStr);
string sqlString = "SELECT * from MWSave WHERE FileName='"+fName+"'";
//label1.Text=sqlString;
 
 
conn.Open();
SqlCommand command = new SqlCommand(sqlString,conn);
SqlDataAdapter adapter = newSqlDataAdapter(command);
SqlDataReader myDataReader = new SqlDataReader();
 
MarketName.Text=myDataReader["FileName"].ToString();
Munoz
Newbie Poster
7 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

You haven't done a read, and you instantiate your reader incorrectly. You don't need a DataAdapter if you're using a Reader.

conn.Open();
SqlCommand command = new SqlCommand(sqlString,conn);
myDataReader = command.ExecuteReader();

while (myDataReader.Read())
{
// do whatever you need to in this loop, including:
// MarketName.Text=myDataReader["FileName"].ToString();
}



Note that if you were really in a loop, the value of your textbox would be set and reset for each record in the results. Likely, you're just returning one row, so wouldn't need the loop. You'd still need to perform a myDataReader.Read() though.

Also, the ExecuteReader() method has on overload where you specify a behavior that it will return a single read, for better efficiency.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

I really appreciate you taking the time. I'm not really familiar with SQL.

The Error I get is: The name 'myDataReader' does not exist in the class or namespace 'System.Text.Reqularexpressions.Admin1'

Am I not declaring it when I say:

myDataReader = command.ExecuteReader();

Munoz
Newbie Poster
7 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 
Am I not declaring it when I say: myDataReader = command.ExecuteReader();

um, that line doesn't appear anywhere in the code you posted

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

Make sure you're using all the relative SQL namespaces.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

I am trying to edit and update the database. I looked at other examples but other people are databinding, but we are just declaring the textboxes. So will this work to edit the database?

privatevoid Button1_Click(object sender, System.EventArgs e)
{

string fName=Request.QueryString["id"];
string connStr = (ConfigurationSettings.AppSettings["dsn_SQL"]);
SqlConnection conn = new SqlConnection(connStr);
string sqlString = "Edit MWSaveBerto SET FileName='"+
MarketName.Text+"' WHERE FileName='"+ fName +"'";
//label1.Text=sqlString;


conn.Open();
SqlCommand command = new SqlCommand(sqlString,conn);
//SqlDataReader myDataReader = command.ExecuteReader();

conn.Close();

Munoz
Newbie Poster
7 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

Thanks!! How do I give a textbox a new value. For Example, Can I give the text box a new value?

Munoz
Newbie Poster
7 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

Instead of all that color formatting, please just use the site's "CODE" tags.

It appears you're designing a form that allows a user to dynamically build a SQL query? I'm sorry, that's a really bad idea. Do a web search on "SQL injection".

To update the value of a textbox, simply do an assignment:

TextBox.Text = "new value";

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You