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
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
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
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
Make sure you're using all the relative SQL namespaces.
tgreer
Made Her Cry
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
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
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37