0

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 = ?????????
7
  • [CompanyName] is from database ? and can there be multiple rows for WHERE [UniqueID] = @UniqueID ? if so will it be okay to set first row's data into textbox ? Commented May 29, 2018 at 12:42
  • yes that is my column name! Commented May 29, 2018 at 12:44
  • @ConstantinosTillits have you successfully query into database ? Commented May 29, 2018 at 12:45
  • no multiple rows. UniqueID is my unique identifier! Commented May 29, 2018 at 12:45
  • there are 10 columns in my table and 10 textboxes on my page and i would like to use them all... i was thinking about filling my textboxes one by one repeating the companyName textbox process 10 times Commented May 29, 2018 at 12:47

2 Answers 2

1
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();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.