Search code examples
c#asp.netms-access

ASP.NET how can I add additional data to database cell without deleting previous data


I have a database which holds informations of students and their lectures. My aim is : Student will select lecture and when they press the button, their selected value will be added to database cell without deleting previous data.

For example: Previous Database

This is the current database. When I click save button the cell deletes its current value and changes his value to the new one. But I want it to not delete the previous value.

This is what I want

For the proccess I am using this command :

 protected void Kaydet_Click(object sender, EventArgs e)
    {

    OleDbConnection baglanti = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:/Users/ERKAN/Desktop/Final/Veritaban.mdb; Persist Security Info = False");

    baglanti.Open();

    OleDbCommand EKLE = new OleDbCommand("UPDATE Ogrenciler SET OgrenciDersler='" + DersList.SelectedItem.ToString() + "' WHERE OgrenciAd='" + Usernamelabel.Text+ "'", baglanti);
   

    OleDbDataReader kayitekle = EKLE.ExecuteReader();

    if (kayitekle.Read())
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
    }
    else
    {
        Response.Write("Kayıt başarısız");
    }

}

I am Not sure what should I use instead of "Update" command. So I am looking for your helps. Also it would be better if I knew how to delete selected value from the database cell.

I am using ASP.NET webforms and as a database access database.


Solution

  • It seems that you want to INSERT a new record into this table instead of UPDATING the old one, e.g.

    INSERT INTO SET OgId, OgrenciDersler VALUES "+ your id + ", '" + DersList.SelectedItem.ToString() + "' 
    

    You may want to have a good read about access SQL commands here before you do too much else!

    https://support.microsoft.com/en-us/office/access-sql-basic-concepts-vocabulary-and-syntax-444d0303-cde1-424e-9a74-e8dc3e460671

    And I would also highly recommend reading up about parameterised queries and why they are important to avoid SQL injection attacks here:

    What is SQL injection?