Count Number Of Records from database Using ExecuteScalar In C#.Net Windows Application. (with code)

preview_player
Показать описание
Learn to Count Number Of Records from database Using ExecuteScalar In C#.Net Windows Application. (with code)

Button Click Method :-

//connect to the database
string str = @"";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd;

//create a command
string query = "SELECT COUNT(brand_id) FROM brands";


try
{
con.Open();
label1.ForeColor = Color.Green;
label1.Text = "Database Sucessfully Connected!!";

cmd = new SqlCommand(query, con);

//read from db
Int32 rows_count = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
con.Close();

//Display data on the page
label2.Text = "No. of Brand = " +rows_count.ToString();

}
catch (Exception ex)
{

Console.WriteLine(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
Рекомендации по теме
Комментарии
Автор

The only video in the ENTIRE Youtube showing a WORKING solution. What a legend! Thank you!

hanostrydom
Автор

Thank you so much !!
Precisava deste código para dar um up no dashboard do meu projecto.

gomilsonotenta
Автор

Just wonderful .... you solved my issue bruh!

jmainex