DataSet ds1 = new DataSet();
SqlDataAdapter sda1 = new SqlDataAdapter(sc1);
sda1.Fill(ds1);
//Second Way - DataReader
sco1.Open();
using (SqlDataReader sdr = sc1.ExecuteReader(CommandBehavior.CloseConnection))
{
//here, if the first field is a string, then 0 is the ordinal of that field and that will be the one returned;
string yourValue = sdr.GetString(0);
}
//Second Way - With a difference (NOTICE the CommandBehaviour)
using (SqlDataReader sdr = sc1.ExecuteReader(CommandBehavior.SingleRow))
{
//here, if the first field is a string, then 0 is the ordinal of that field and that will be the one returned;
string yourValue = sdr.GetString(0);
}
qwekovaqwe
ASKER
sorry i am not sure which one to use
i need something like (pseudo code)
for each record returned
{
currentrecordcolumn1 = get value of a first column filed from the current column;
currentrecordcolumn2 = get value of a second column filed from the current column;
etc...
}
hope you understand.
i think this is the one i need to use inside a loop (if this is how to loop through all records?)
string yourValue = sdr.GetString(0); << do these continue, 0,1,2,3,etc... << is it possile to use names instead of indexes?
The solution from quathampj is the fastest and least memory consuming.
But do the GetOrdinal before the sdr.Read() and put the integer result in a meaningful variable.
And be sure to check the sdr.IsNull(theOrdinalForcolum1) before GetString() or any other cast.
The solution of REA_ANDREW is the easiest to handle though... and datasets and datatables can easily be used as datasource for grids, comboboxes, listboxes and other databindable components.
// try?
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(hrDataSource); // !!!!!!!!!!!!!!!!!!!! THIS LINE GIVES AN ERROR
sda.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
TEST.Text=dr["ShortName"].ToString();
}
TEST is a label - there should be only 1 result using this stored procedure
!!!! line with an error:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1502: The best overloaded method match for 'System.Data.SqlClient.SqlDataAdapter.SqlDataAdapter(System.Data.SqlClient.SqlCommand)' has some invalid arguments
Source Error:
Line 52: SqlDataAdapter sda = new SqlDataAdapter(hrDataSource);
@reaandrew
SqlDataAdapter sda1 = new SqlDataAdapter(sc1); < i was trying to do this
can it be adapted to my use
also, what is sco1?
Gautham Janardhan
try this
System.Data.SqlClient.SqlConnection FConnection =
new System.Data.SqlClient.SqlConnection
(ConfigurationManager.
ConnectionStrings["hrConnectionString"].ToString());
System.Data.SqlClient.SqlDataAdapter FAdpapter =
new System.Data.SqlClient.SqlDataAdapter
("selectEntityInformationByUID"
,FConnection);
FAdpapter.SelectCommand.Parameters.Add("UID", uid);
DataSet ds = new DataSet();
FAdpapter.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
TEST.Text=dr["ShortName"].ToString();
}
SqlCommand sc1 = new SqlCommand();
sc1.Connection = sco1;
sc1.CommandType = CommandType.StoredProcedur
sc1.CommandText = "YourProc";
sc1.Parameters.AddWithValu
//FirstWay - DataAdapter & DataSet
DataSet ds1 = new DataSet();
SqlDataAdapter sda1 = new SqlDataAdapter(sc1);
sda1.Fill(ds1);
//Second Way - DataReader
sco1.Open();
using (SqlDataReader sdr = sc1.ExecuteReader(CommandB
{
//here, if the first field is a string, then 0 is the ordinal of that field and that will be the one returned;
string yourValue = sdr.GetString(0);
}
//Second Way - With a difference (NOTICE the CommandBehaviour)
using (SqlDataReader sdr = sc1.ExecuteReader(CommandB
{
//here, if the first field is a string, then 0 is the ordinal of that field and that will be the one returned;
string yourValue = sdr.GetString(0);
}