0
votes

i am coding for a commenting system in asp.net C# but i am stopped at delete command because of i am not using any type of serial numbers to comments posted, then how can i able to delete a specific comment, i am just using a username, date, time, and text in comment. Can anyone help me please that how to use a delete command in this condition??

here is my code for posting: protected void pospost_Click(object sender, EventArgs e) {

    string login;
    if (HttpContext.Current.Session["UserName"] != null)
    {
        login = HttpContext.Current.Session["UserName"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "select * from mobiles_pos";
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);

        DataRow rw = ds.Tables[0].NewRow();
        rw[0] = Model.Text.ToString();
        rw[1] = titlepos.Text.ToString();
        rw[2] = txtpos.Text.ToString();
        rw[3] = DateTime.Today.Date.ToString();
        rw[4] = DateTime.Now.TimeOfDay.ToString();
        rw[5] = login.ToString();

        ds.Tables[0].Rows.Add(rw);
        SqlCommand cmd1 = new SqlCommand();
        cmd1.Connection = con;
        cmd1.CommandText = "insert into mobiles_pos values('" + Model.Text + "','" + titlepos.Text + "','" + txtpos.Text + "','" + DateTime.Today.Date + "','" + DateTime.Now.TimeOfDay + "','" + login + "')";
        da.InsertCommand = cmd1;
        da.Update(ds);

        con.Close();

        titlepos.Text = "";
        txtpos.Text = "";
        //DataList2.DataSource = ds;
        //DataList2.DataBind();
        BindDataList2();
    }
}
1
Add a primary key to your table (auto generated int or guid) and then you'll have something to use in your delete command.lucuma

1 Answers

1
votes

Best - Add a Primary key to the "mobiles_pos" table since your using sql just use an identity field it will auto increment for you.

or

Quick - Use a combination of the User name and date comment was intered you must use the full date time or it will delete everything that user entered that day.

"Delete from mobiles_pos where username = @UserName and createdDate = @createdDate"