Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulate Datatable - Row with Data from Sql-Command with several selectquery
    text
    copied!<p>I have the following function:</p> <pre><code>private static DataTable GetDataFromDatabase(DateTime myDate) { DataTable table = new DataTable(); table.Columns.AddRange(new DataColumn[] { new DataColumn("Event"), new DataColumn("Room"), new DataColumn("From"), new DataColumn("To"), new DataColumn("Persons"), new DataColumn("Equipment"), new DataColumn("Catering") }); string strSQL = "SELECT distinct ID FROM VIEW_RAUMBUCHUNG_DISPO " + "WHERE BOOKSTATUS &gt;= 1 AND convert(char, von,104) = '" + BookITVbSQL.to_104(myDate) + "'"; SqlDataReader objRS; objRS = SQLrunReaderDP(strSQL); while (objRS.Read()) { using (SqlConnection con = new SqlConnection(GetConnectionString())) { using (SqlCommand cmd = con.CreateCommand()) { con.Open(); cmd.CommandText = "SELECT distinct EVENT, ROOM, CONVERT(char(5), from, 108) " + "FROM, CONVERT(char(5), to, 108) TO, PERSONS FROM VIEW_RAUMBUCHUNG_DISPO " + "WHERE ID = '" + objRS["ID"] + "'; " + "SELECT EQUIPMENTNAME FROM EQUIPMENT WHERE BUCHUNG_ID = '" + objRS["ID"] + "' and STATUS = '2'; " + "SELECT CATERINGNAME FROM CATERING WHERE BUCHUNG_ID = '" + objRS["ID"] + "' and STATUS = '1';"; using (SqlDataReader rdr = cmd.ExecuteReader()) { do { while (rdr.Read()) { table.Rows.Add( rdr["EVENT"], rdr["ROOM"], rdr["FROM"], rdr["TO"], rdr["PERSONS"] ); } } while (rdr.NextResult()); rdr.Close(); } con.Close(); } } } return table; } </code></pre> <p>This works fine for the first Statement. I can get all the Data from the five Columns. But how can i Add the Columns from the other two Querys to the same Row?</p> <p>If i try:</p> <pre><code> while (rdr.Read()) { table.Rows.Add( rdr["EVENT"], rdr["ROOM"], rdr["FROM"], rdr["TO"], rdr["PERSONS"], rdr["EQUIPMENTNAME"]); } </code></pre> <p>so iam getting an IndexOutOfRange - Exception. Can someone help me with that please?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload