Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wrote a record to count the record value;
and these count value will show on the lable;
i write these code.....


SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["sst"].ToString());
con2.Open();
SqlCommand c1 = new SqlCommand("Select COUNT(hosting) from mastertable where [user]='" + lblnam.Text + "'", con2);
SqlDataAdapter da1 = new SqlDataAdapter(c1.CommandText, con2);
DataSet ds1 = new DataSet();

da1.Fill(ds1);
lbhosttake.Text = ds1.Tables[0].Rows.Count.ToString();
con2.Close();

here lbhosttake is a lable that will show the count value.. Actually as i run it shows the count value only 1.
Weather the record is avaiable or not, weather has many record,, but it shows only 1 every time..

what can i do. so that it will show total counting value.
Posted

1 solution

use SqlCommand.ExecuteScalar Method[^]

C#
con2.Open();
SqlCommand c1 = new SqlCommand("Select COUNT(hosting) from mastertable where [user]='" + lblnam.Text + "'", con2);
int count=(int)c1.Executescalar();
lbhosttake.Text =count.ToString();


Select COUNT(hosting) from .. return only single value ( one row), so the row count will be one. You need to read the value of the return row with Executescalar method or read the value of ds1.Tables[0].Rows[0].Cels[0] value
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900