Avatar of qwekovaqwe
qwekovaqwe
 asked on

c# select

SqlDataSource hrDataSource = new SqlDataSource();
hrDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["hrConnectionString"].ToString();

hrDataSource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
hrDataSource.SelectCommand = "selectEntityInformationByUID";

hrDataSource.SelectParameters.Add("UID", uid);



how to execute this one & how to read the results from the select?
similarly - with insert i use

int rowsAffected = 0;
                  
try
{
  rowsAffected = hrDataSource.Insert();
}
  catch (Exception ex)
{
  // log an error
}
finally
{
  hrDataSource = null;
}

HOW TO DO IT FOR SELECT? (if there is one result, and if there are more than 1)
thnx
.NET ProgrammingC#

Avatar of undefined
Last Comment
qwekovaqwe

8/22/2022 - Mon
REA_ANDREW

You could try this way.

                  SqlCommand sc1 = new SqlCommand();
                sc1.Connection = sco1;
                sc1.CommandType = CommandType.StoredProcedure;
                sc1.CommandText = "YourProc";
                sc1.Parameters.AddWithValue("@UID", "YourValue");

                //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(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?


thnx a lot
ASKER CERTIFIED SOLUTION
Gautham Janardhan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
REA_ANDREW

Well that is one way OR using your SqlDataAdapter approach you could do this:

                  SqlCommand sc1 = new SqlCommand();
                sc1.Connection = sco1;
                sc1.CommandType = CommandType.StoredProcedure;
                sc1.CommandText = "YourProc";
                sc1.Parameters.AddWithValue("@UID", "YourValue");

                //FirstWay - DataAdapter & DataSet

                DataSet ds1 = new DataSet();
                SqlDataAdapter sda1 = new SqlDataAdapter(sc1);
                sda1.Fill(ds1);

foreach(DataRow dr in ds1.Tables[0].Rows)
{
string yourString = dr["YourColumn"].ToString();
}
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
illusio

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.

Kind regards,
Peter
qwekovaqwe

ASKER
here is what i have so far:

// read query string
string uid = Request.QueryString.Get("ID");

// database select
SqlDataSource hrDataSource = new SqlDataSource();
// connection string
hrDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["hrConnectionString"].ToString();
            
hrDataSource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
hrDataSource.SelectCommand = "selectEntityInformationByUID";
            
// parameters
hrDataSource.SelectParameters.Add("UID", uid);
            
// 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();
}

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Gautham Janardhan

and before   FAdpapter.Fill(ds);


do this also
            FAdpapter.SelectCommand.CommandType =
                        System.Data.CommandType.StoredProcedure;
qwekovaqwe

ASKER
i have managed to do it following your suggestions
thank you very much