0

My issue is that everytime I try to update in my CMS, it simply updates EVERY column / row. For example: I update Test 1 -> Test 2

Meanwhile I have Test 4

But then Test 4 -> Test 2 due to I changed Test 1 to 2 it simply changes that aswell.

They still work by ID correctly, and when I delete them, they delete individually, so the only function that is overlapping with everything is update.

I will now post my code (this is what I have learned, do not comment on security or w/e) simply need this issue fixed so it only updates the selected ID row.

First:

    public DataRow GetById(string ID)
{

    strSQL = "SELECT ID, clnOverskrift, clnTekst "
          + "FROM tblForside "
          + "WHERE ID=@ID";

    objCMD = new MySqlCommand(strSQL);
    objCMD.Parameters.AddWithValue("@ID", ID);

    return objData.GetData(objCMD, objCon).Rows[0];
}
public void Update(PropertyForside Pro)
{

    strSQL = "UPDATE tblForside SET "
             + " clnOverskrift=@Overskrift, clnTekst=@Tekst ";

    objCMD = new MySqlCommand(strSQL);
    objCMD.Parameters.AddWithValue("@Overskrift", Pro.Overskrift);
    objCMD.Parameters.AddWithValue("@Tekst", Pro.Tekst);

    objData.ModifyData(objCMD, objCon);
}

I will use both GetById and Update thats why I included both. if you need to know more in this "factory" i will post it.

Here i get it:

    FactoryForside fac = new FactoryForside();
PropertyForside Pro = new PropertyForside();

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        DataRow dr = fac.GetById(Request.QueryString["ID"]);

        txtOverskrift.InnerText = dr["clnOverskrift"].ToString();
        txtText.InnerText = dr["clnTekst"].ToString();
    }
}
protected void btnGem_Click(object sender, EventArgs e)
{
    Pro.Overskrift = txtOverskrift.InnerText;
    Pro.Tekst = txtText.InnerText;
    Pro.ID = int.Parse(Request.QueryString["ID"]);

    fac.Update(Pro);

    Response.Redirect("RedigerForside.aspx");
}

I use InnerText to use TextAreas with NiceEdit.

Adding new ones work, and deleting as mentioned before - only this updating thing not working correctly. It displays ID correctly "EditForside.aspx?id=13" in the browser, but it seems to select every other ID aswell

Hope you can help me.

3 Answers 3

2

Your UPDATE statement has to include a WHERE condition - the same you use in your select. Then it will update only that particular row. Without a WHERE it updates every row in the table.

Documentation: http://dev.mysql.com/doc/refman/5.0/en/update.html

Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU. I mean.. This is one of the most basic things, and I missed it. I am ********* Thank you once again!
0

The reason might be because you have not provided the where condition in your update clause. So if you dont provide a where condition in your update statement it would end up updating every row of your table.

Try something like this:

strSQL = "UPDATE tblForside SET "
         + " clnOverskrift=@Overskrift, clnTekst=@Tekst "
         + " WHERE ID=@ID";

Comments

0

You need to provide a WHERE clause in your update statement. E.g.

strSQL = "UPDATE tblForside SET "
         + " clnOverskrift=@Overskrift, clnTekst=@Tekst "
         + " WHERE ID=@ID";

This limits which rows will be updated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.