showing multiple rows in a table after an SQL query
-
Hi everyone First I'm trying to return multiple rows from a table via XML:
string strSQLCommand = "SELECT \* FROM Magazalar WHERE Sehir = '" + a + "'"; SqlCommand command = new SqlCommand(strSQLCommand, conn); TableRowCollection returnvalue = (TableRowCollection)command.ExecuteScalar();
a is cannot be null and the query may return a single row or multiple rows. then, I want to show this returned row collection in a table:
MagazaShowTable.Rows = returnvalue;
The compiler says that Table.Rows is a Read-Only, and sth cannot be assigned to it, and I couldn't find any suitable option rather than .Rows ... Can anybody help? -
Hi everyone First I'm trying to return multiple rows from a table via XML:
string strSQLCommand = "SELECT \* FROM Magazalar WHERE Sehir = '" + a + "'"; SqlCommand command = new SqlCommand(strSQLCommand, conn); TableRowCollection returnvalue = (TableRowCollection)command.ExecuteScalar();
a is cannot be null and the query may return a single row or multiple rows. then, I want to show this returned row collection in a table:
MagazaShowTable.Rows = returnvalue;
The compiler says that Table.Rows is a Read-Only, and sth cannot be assigned to it, and I couldn't find any suitable option rather than .Rows ... Can anybody help?Hi, first of all be aware that command.ExecuteScalar() returns the first column from the first row, not multiple rows. Then as the compiler already says, Rows is a ReadOnly-Property. You could add the rows by using MagazaShowTable.Rows.Add
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
-
Hi, first of all be aware that command.ExecuteScalar() returns the first column from the first row, not multiple rows. Then as the compiler already says, Rows is a ReadOnly-Property. You could add the rows by using MagazaShowTable.Rows.Add
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
Thanks for answering... So what is the suitable function for calling multiple rows? command.ExecuteReader() ? I'm converting the returned value to TableRowCollection, is it right? and how I can I show this row collection in my table?
-
Thanks for answering... So what is the suitable function for calling multiple rows? command.ExecuteReader() ? I'm converting the returned value to TableRowCollection, is it right? and how I can I show this row collection in my table?
ExecuteReader is what you need. The returned value is a SqlDataReader. With this you can iterate through the rows. Simpler approach is to use a dataset with a dataadapter. This will read the rows within a table anyway. But first you can use ExecuteReader, iterate through the rows and add the rows with values to your datatable.
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
-
ExecuteReader is what you need. The returned value is a SqlDataReader. With this you can iterate through the rows. Simpler approach is to use a dataset with a dataadapter. This will read the rows within a table anyway. But first you can use ExecuteReader, iterate through the rows and add the rows with values to your datatable.
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
I'll give it a try thank you! :D