0

I want to fill in the first column of data from a DataTable into a textbox.

What is the syntax to do that.

I have this so far, but get an error on the last line.

    DataTable dt = new DataTable();
    string strConnection = ConfigurationManager.ConnectionStrings["ChinatowndbConnString"].ConnectionString;
    SqlConnection conn = new SqlConnection(strConnection);
    string sql = "SELECT * FROM vwSchedule where scheduleid =" + scheduleid;
    SqlCommand cmd = new SqlCommand(sql);
    cmd.CommandType = CommandType.Text;
    cmd.Connection = conn;
    SqlDataAdapter sd = new SqlDataAdapter(cmd);
    sd.Fill(dt);

    tbEvent.Text dt.Rows[0].Field<String>(0); // first field of first row, assuming that it's a string

Regards Tea

TeaDrinkingGeek
  • 1,995
  • 5
  • 34
  • 52
  • So what's not working? If you replace `tbEvent.Text dt.Rows[0].Field(0);` with `tbEvent.Text=dt.Rows[0].Field(0);` it should work as expected (assuming that there are rows and that the first column's type is string). – Tim Schmelter Nov 12 '12 at 14:37
  • could it be that coulmn 0 is not a string – AMember Nov 12 '12 at 14:39
  • My mistake, I forgot the = sign. – TeaDrinkingGeek Nov 12 '12 at 14:39
  • The exact line of error would have been pointed out by the compiler itself. A little introspection would have helped discover the obviously missing Equals sign. – tempidope Nov 12 '12 at 14:44
  • @JKarthik: However, not worth a downvote since the question is clear. But it should be closed as _"too localized (unlikely to help any future visitors)"_. – Tim Schmelter Nov 12 '12 at 14:46
  • The reason was "The question does not show any research effort". If every compiler error question is logged on StackOverflow, the site would indeed overflow :) Agree with your point too though. – tempidope Nov 12 '12 at 14:51

2 Answers2

1

If you replace

tbEvent.Text dt.Rows[0].Field<String>(0);

with

tbEvent.Text = dt.Rows[0].Field<String>(0);

it should work as expected (assuming that there are rows and that the first column's type is string).

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

You're missing an Equals in the last line syntax:

tbEvent.Text dt.Rows[0].Field<String>(0);

It should have been:

tbEvent.Text = dt.Rows[0].Field<String>(0); 

Or

tblEvent.Text = dt.Rows[0].ItemArray[0];

That should do it.

Reference: Access cell value of datatable

Community
  • 1
  • 1
tempidope
  • 823
  • 1
  • 12
  • 29