Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Friends,
I need your help, please.
I want to Count 'public schools' in each State(which is a column name) across all 50 States in USA(currently having 50 to 100 rows in sql server table named "tbl_PublicSchoolUSA") and display in 50 TextBox for each State using C# asp Dot Net Sql Server 2008

SELECT States, COUNT(States) AS Total_Schools
FROM tbl_PublicSchoolUSA
GROUP BY States
HAVING (COUNT(States) >= 1)

After all these days of "hard work(?)" had led nowhere.
Please help.
Thanks,
Ravi

What I have tried:

I tried the above SQL statement in Sql server 2008 - It works well - but, how can I display each value in each TextBox in ASP.Net.

This is my C# code

private void DisplayInTextBox()
{
string constring = (@"Data Source=.\....bla...bla..bla");
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT States, COUNT(States) AS Total_Schools FROM tbl_PublicSchoolUSA GROUP BY States HAVING (COUNT(States) >= 1)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
TextBox1.Text = sdr["Total_Schools"].ToString();////Total schools in California
TextBox2.Text = sdr["Some Alias Name"].ToString(); ///Total schools in New York
TextBox3.Text = sdr["Some Alias Name"].ToString(); ///Total schools in Ohio
TextBox4.Text = sdr["Some Alias Name"].ToString(); ///Total schools in Illinois
...............
...........
.........
}
con.Close();
}
}
}
Posted
Updated 12-Jun-22 8:20am

1 solution

Don't. Use a DataGridView instead.

Use an SqlDataAdapter instead of the SqlDataReader to load the state and count into a DataTable, then set that as the DataSource for a DataGridView and it will organise it for you in a much more flexible way.
 
Share this answer
 
Comments
ravitv 13-Jun-22 12:14pm    
Yes OriginalGriff - Thanks for the clue.
I tried this code and works well,

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\....bla....bla....bla...");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT States, COUNT(States) AS Total_Schools FROM tbl_PublicSchoolUSA GROUP BY States HAVING (COUNT(States) >= 1)",conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

foreach (DataRow row in dt.Rows)
{
TextBox3.Text = dt.Rows[0][0].ToString();
TextBox4.Text = dt.Rows[1][0].ToString();
TextBox5.Text = dt.Rows[2][0].ToString();
TextBox6.Text = dt.Rows[3][0].ToString();
}
}

but I want the count value to be displayed in "dynamically generated TextBox"
for the States that has some rows and SHOULD NOT display the TextBox that DOESNT have any rows.
(i.e) I want to display values in the TextBox for California,New York,Ohio,Illinois only.(other states are still under data entry)
So if I get any records entered in "tbl_PublicSchoolUSA" in "States" Column for Florida,Idaho then the values for Florida,Idaho
should be displayed along with other 4 States and so and so forth...
How can I achieve this.
Please help.
Thanks
Ravi.

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