Rares
Rares

Reputation: 97

assign data from table to labels using c#

I'm using c# in a ASP.Net web application.I have the following query:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chestionar"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from personal,Intrebari where personal.cod_numeric_personal=@cnp AND Intrebari.id_intrebare=14 AND Intrebari.id_intrebare=15 ", con);
cmd.Parameters.AddWithValue("@cnp", Session["sesiune_cnp"]);
SqlDataReader rdr;

rdr = cmd.ExecuteReader();

while (rdr.Read())
{

    lbl1.Text = rdr["Nume"].ToString();
    intrebare6.Text = rdr["Intrebari"].ToString();
    intrebare7.Text = rdr["Intrebari"].ToString();

}

I want those two values for id_intrebare=14 and 15 to assign it to those 2 labels.How can i refer to those?

Upvotes: 0

Views: 3452

Answers (4)

Zo Has
Zo Has

Reputation: 13018

Try this, notice the try catch block, I also changed your SQL query.

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chestionar"].ConnectionString);
string qry="select * from personal,Intrebari where personal.cod_numeric_personal=@cnp AND Intrebari.id_intrebare IN (14,15);

SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@cnp", Session["sesiune_cnp"]);
try
{  
   con.Open();    
   SqlDataReader rdr= cmd.ExecuteReader();
   if(rdr.HasRows)
   {    
        while (rdr.Read())
        {
            lbl1.Text = rdr["Nume"].ToString();
            intrebare6.Text = rdr["Intrebari"].ToString();
            intrebare7.Text = rdr["Intrebari"].ToString();
        }
   }
}
catch(SQLException ex)
{
   lblStatus.Text="An error occured"+ex.Message;
   throw ex;
}
finally
{
   con.Close();
   con.Dispose();
}

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112334

If you want to assign texts to different numbered lables in a loop, you can refer to the control id with FindControl of the current page

int numeOrdinal = reader.GetOrdinal("Nume");
int intrebariOrdinal = reader.GetOrdinal("Intrebari");
int i = 1;
while (rdr.Read()) {
    // Nume (Romanian) = Name
    page.FindControl("lbl" + i).Text = reader.IsDBNull(numeOrdinal)
        ? ""
        :  rdr.GetString(numeOrdinal);

    // Intrebari (Romanian) = Question
    page.FindControl("intrebari" + i + 5).Text = reader.IsDBNull(intrebariOrdinal)
        ? ""
        : rdr.GetString(intrebariOrdinal);

    i++;
}

Upvotes: 0

dice
dice

Reputation: 2880

In order to read stuff from the reader you need to include it in the select statement for you sql, it is better to select it explicitly rather than use select *.

but you are not currently going to get any results returned because id_intrebare cannot be both 14 and 15

you then need to read id_intreabare ratherr than Intreabari.

Upvotes: 3

NDraskovic
NDraskovic

Reputation: 706

Try using cmd.ExecuteScalar it will return the first reuslt it finds so you have to define your conditions well. Also it returns object type so you will have to cast the result

Upvotes: -1

Related Questions