简体   繁体   中英

Check if record already exists within the database

I have a program that allows users to enter in movies and details about that movie and store them in a database. I have the database being displayed in my C# program and the user can select one of the rows and the information from that row will be put into the text box it corresponds to, for example the Title will go in the title text box and so on. what I want to do is stop the user hitting the submit button and putting the same record in the database.

Any help would be appreciated

Submit button:

private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtTitle.Text) || string.IsNullOrWhiteSpace(txtRunTime.Text)) 
            {
                MessageBox.Show("Fill in all the required fields"); 
            }
            else 
            {

                if (lstStatus.SelectedIndex == 0) 
                {
                    Status = "Watched"; 
                }
                else 
                {
                    Status = "Not Watched"; 
                }

                if (lstType.SelectedIndex == 0) 
                {
                    Type = "Movie"; 
                }
                else 
                {
                    Type = "TV Show";
                } 

                con.Open(); 
                SqlCommand cmd = new SqlCommand("insert into dbo.Movies(Title, Genre, RunTime, Type, Status) values('"+txtTitle.Text+"','"+txtGenre.Text+"','"+txtRunTime.Text+"','"+Type+"','"+Status+"')", con); 
                cmd.ExecuteNonQuery(); 
                MessageBox.Show("Data transferred into the database!"); 
                con.Close(); 

                txtTitle.Text = ""; 
                txtRunTime.Text = ""; /
            } 
        } 

Code when selecting a row:

private void DataGridMovies_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if(DataGridMovies.CurrentRow.Index == DataGridMovies.Rows.Count - 1)
            {
                MessageBox.Show("Empty row selected"); // Display a message to the user
            }
            else
            {
                ID = Convert.ToInt32(DataGridMovies.Rows[e.RowIndex].Cells[0].Value.ToString());
                txtTitle.Text = DataGridMovies.Rows[e.RowIndex].Cells[1].Value.ToString();
                txtGenre.Text = DataGridMovies.Rows[e.RowIndex].Cells[2].Value.ToString();
                txtRunTime.Text = DataGridMovies.Rows[e.RowIndex].Cells[3].Value.ToString();
                if (DataGridMovies.Rows[e.RowIndex].Cells[4].Value.ToString() == "Movie")
                {
                    lstType.SelectedIndex = 0;
                }
                else
                {
                    lstType.SelectedIndex = 1;
                }// End of IF ELSE Statement
                if (DataGridMovies.Rows[e.RowIndex].Cells[5].Value.ToString() == "Watched")
                {
                    lstStatus.SelectedIndex = 0;
                }
                else
                {
                    lstStatus.SelectedIndex = 1;
                }// End of IF ELSE Statement
            }//End of IF statement

You need to use sql parameters or your sql database can be hacked.

You can read about that here: https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.parameters?view=netframework-4.8 cmd.Parameters.AddWith(name, value)

Determine if the record exists. If it does not you are adding it with insert. If not you update the record instead.

Depending on the process power you have at your disposal you might also look into autocomplete. This can hide the fact they are just updating a record you already "found". https://www.grapecity.com/blogs/dynamic-autocomplete-c1textbox

Meta tagging and algorithms on the back end sometimes take care of duplicate entries.

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