c# - Update a winforms label or textbox using database data -
I want to get some text from the database and use this data to update the value of the label or textbox I am My only experience has been binding on a gridview so far, but this method does not work in this scenario. Can you please advise?
Here's a tough example. In the real world, you want to reuse your connection object / perhaps use database physics and connectionpooling. This example only shows an easy way to populate label text with information from the database.
const string connection string = "initial catalog = northwind; data source = localhost; integrated security = sspi;"; Const string GetLabelText = "select labeltext from myLabelTextTable where id = {0}"; Const String DefaultLabelText = "-define-"; Public Zero UpdateLabel (label myLabel, int labelstest ID) {string labelText; (Using SqlConnection Connection = New SqlConnection (ConnectionString)) (using connection.Open (); (SqlCommand command = new System.Data.SqlClient.SqlCommand (string.Format (GetLabelText, LabelTextId, connection)) {Label text = (command.ExecuteScalar () ?? DefaultLabelText) .restring ();} Connection.Close ();} MyLabel.Text = labelText;}
Comments
Post a Comment