Frosty
Frosty

Reputation: 39

SQLConnection bringing data to TextBox

I know this looks really simple but i've been looking for an answer for hours with no luck.

I want to fill my row values into a bunch of textboxes. How can I specify that [CompanyName] is going to be used by the companyName textbox? Please keep it as simple as possible (beginner level).

string customerUniqueID = "test";

string constr = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString(); // connection string
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand com = new SqlCommand("SELECT * FROM [Customers] WHERE [UniqueID] = @UniqueID", con); // table name 
com.Parameters.Add("@UniqueID", SqlDbType.Int);
com.Parameters["@UniqueID"].Value = customerUniqueID;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
companyName.Text = ?????????

Upvotes: 0

Views: 1609

Answers (2)

SᴇM
SᴇM

Reputation: 7213

I will recommend some changes in your code:

  1. Your sql query returning result from one set, so you can use DataTabe instead of DataSet.
  2. To fill results from DB to your DataTable you can use SqlAdapter.Fill() method.
  3. Use Field() generic method (more examples of Field()) to get values from your DataTable.
  4. Use using blocks for disposable objects, or at least make sure you've closed them after.
  5. There is no need of con.Open() to open connection when using Fill() method, because from MSDN:

The Fill method implicitly opens the Connection that the DataAdapter is using if it finds that the connection is not already open. If Fill opened the connection, it will also close the connection when Fill is finished. This can simplify your code when dealing with a single operation such as a Fill or an Update.

string customerUniqueID = "test";
string constr = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString(); // connection string
using(SqlConnection con = new SqlConnection(constr))
{
    SqlCommand com = con.CreateCommand();
    com.CommandText = "SELECT * FROM [Customers] WHERE [UniqueID] = @UniqueID";
    com.Parameters.Add("@UniqueID", SqlDbType.Int);
    com.Parameters["@UniqueID"].Value = customerUniqueID;
    using(SqlDataAdapter da = new SqlDataAdapter(com))
    {
        DataTable dt = new DataTable();
        da.Fill(dt);
        companyName.Text = dt.Rows[0].Field<string>("CompanyName");
    }
}

Please feel free to comment, if I missed something.

Upvotes: 1

mbadeveloper
mbadeveloper

Reputation: 1270

string customerUniqueID = "test";

string constr = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString(); // connection string
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand com = new SqlCommand("SELECT * FROM [Customers] WHERE [UniqueID] = @UniqueID", con); // table name 
com.Parameters.Add("@UniqueID", SqlDbType.Int);
com.Parameters["@UniqueID"].Value = customerUniqueID;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds, "Customers");

companyName.Text = ds.Tables[0].Rows[0]["CompanyName"].ToString();

Upvotes: 1

Related Questions