简体   繁体   中英

How To count updated same record row in db and show count number in label?

Here i have table name tblbb which have a column called journalname, textbox ,button and a label.I have store some data in tblbb. Now what i am trying to do is, to count same data row in column journalname when i input data data in textbox and on button click show the count in label. for eg in tblbb

Journalname

aabb if textbox.text="a" label.text=2 But the problem is that when i update data in table the label text does not show the updated count for that data.For example if i store one more a in above table then on button click the must show lable.text=3,but instead of that it shows label.text=2 even the table is updated.

The method used

   public DataTable getjournalcount(string journalname)
{
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
    string sql = "select journalname, count(*) as dupes from tblbb group by journalname";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@JournalName", journalname);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
}

Code behind button

   protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt = j.getjournalcount(TextBox1.Text);
    if (dt.Rows.Count>0)
    {
        Label1.Text= dt.Rows[0]["dupes"].ToString();

    }

}

modify your sql as

string sql = "select journalname, count(*) as dupes 
from tblbb where where journalname=@journalname 
group by journalname";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM