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.
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.
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();
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.
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();
Am I not declaring it when I say:
myDataReader = command.ExecuteReader();
um, that line doesn't appear anywhere in the code you posted
Make sure you're using
all the relative SQL namespaces.
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?
private void 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();
Thanks!! How do I give a textbox a new value. For Example, Can I give the text box a new value?
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";
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.