locked
Populate a string with a dataTable

    Soru

  • Hello everyone.  I am trying to populate a string with the values in a dataTable as I want to use this in a string. Does anyone know how I can do this?  I have a result of 2 columns and maybe 10 rows and I want to place it in a string so that I can put it in text boxes and stuff.

    This is the code that I am using here.

                    con = new System.Data.SqlClient.SqlConnection();
                    ds1 = new DataSet();
    
                    con.ConnectionString = DBAccessString;
    
                    string sql = "SELECT ID, Question, Answer FROM JobsQuestions WHERE JobID = '" + Job + "' ORDER BY ID Asc";
    
                    da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
    
                    con.Open();
    
                    da.Fill(ds1, "Script");
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    //ScriptDGV.DataSource = dt;
                    ScriptString = dt.ToString();
    
                    MessageBox.Show(ScriptString, "SHDev");
    
                    try
                    {
                        DataRow dRow = ds1.Tables["Script"].Rows[0];
                    }
                    catch
                    {
    
                    }
    
                    con.Close();
    
                    con.Dispose();

    Thank you again for the help. :)

    28 Şubat 2012 Salı 18:53

Yanıtlar

  • You can use datatable itself to show data in textbox.

     Txtbox1.Text = dt.Rows[0]["Col"].ToString();


    Amit Govil | Email

    "Weeks of coding can save you hours of planning"

    28 Şubat 2012 Salı 19:04
  • I've never done anything like this.  Maybe use a foreach and go through the items that are in the DataRow?

    Maybe something like this?

                    foreach (DataRow dr in ds1.Tables["Script"].Rows)
                    {
                        ScriptString += "Q) " + dr["Question"].ToString() + "   A) " + dr["Answer"].ToString() + Environment.Newline;
                    }

    I hope that helps.
    • Yanıt Olarak Öneren MLyons10 01 Mart 2012 Perşembe 16:52
    • Yanıt Olarak İşaretleyen Lie YouModerator 05 Mart 2012 Pazartesi 02:49
    28 Şubat 2012 Salı 20:38

Tüm Yanıtlar

  • You can use datatable itself to show data in textbox.

     Txtbox1.Text = dt.Rows[0]["Col"].ToString();


    Amit Govil | Email

    "Weeks of coding can save you hours of planning"

    28 Şubat 2012 Salı 19:04
  • I've never done anything like this.  Maybe use a foreach and go through the items that are in the DataRow?

    Maybe something like this?

                    foreach (DataRow dr in ds1.Tables["Script"].Rows)
                    {
                        ScriptString += "Q) " + dr["Question"].ToString() + "   A) " + dr["Answer"].ToString() + Environment.Newline;
                    }

    I hope that helps.
    • Yanıt Olarak Öneren MLyons10 01 Mart 2012 Perşembe 16:52
    • Yanıt Olarak İşaretleyen Lie YouModerator 05 Mart 2012 Pazartesi 02:49
    28 Şubat 2012 Salı 20:38