Click here to Skip to main content
16,004,647 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the problem with my code is when i click save it insert duplicate records

idont know how i fix that

C#
 using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;
    namespace Carprogram
    {
        public partial class Form1 : Form
        {
            SqlConnection mm = new SqlConnection("Data Source=NAWAF;Initial Catalog=CAR;Integrated Security=True");
            DataTable dt = new DataTable();
            public Form1()
            {
                InitializeComponent();
            }

        
        string imgloc="";
        SqlCommand cmd;
         private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
          OpenFileDialog dialog = new OpenFileDialog ();
             dialog.Filter = "png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|All files(*.*)|*.*";
             if(dialog.ShowDialog()==DialogResult.OK)
             {
                 imgloc =dialog.FileName.ToString();
                 pictureBox1.ImageLocation=imgloc;

             }
        }


        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void save_Click(object sender, EventArgs e)
        {
            string stat = "active";

            byte[] images = null;
            FileStream Streem = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
            BinaryReader brs = new BinaryReader(Streem);
            images = brs.ReadBytes((int)Streem.Length);

            
            mm.Open();

            string vmd = "Insert into vehicleinfo(modely,make,model,color,type,odometer,odoty,vin,vehicle,driverop,department,engine,transmission,tiresize,platlic,renewal,company,account,premium,due,note,img,status)VALUES('" + modely.Text + "' ,'" + make.Text + "' , '" + model.Text + "' , '" + color.Text + "' , '" + type.Text + "','" + odometer.Text + "' ,'" + odoty.Text + "','" + vin.Text + "' , '" + vehicle.Text + "' , '" + driverop.Text + "' , '" + department.Text + "','" + engine.Text + "','" + transmission.Text + "','" + tiresize.Text + "','" + platlic.Text + "','" + renewal.Text + "','" + company.Text + "','" + account.Text + "','" + premium.Text + "','" + due.Text + "','" + note.Text + "',@images,@stat)";
           
           cmd = new SqlCommand (vmd,mm);
            cmd.Parameters.Add(new SqlParameter("@images",images));
            cmd.Parameters.Add(new SqlParameter("@stat", stat));

             cmd.ExecuteNonQuery();

             int N = cmd.ExecuteNonQuery();
            

            mm.Close();

            MessageBox.Show(N.ToString() + "Data Saved");



            modely.Text ="";
             make.Text ="";
             model.Text ="";
             color.Text ="";
             type.Text ="";
              odometer.Text=""; 
              vin.Text ="";
             vehicle.Text ="";
             driverop.Text ="";
             department.Text ="";
              engine.Text ="";
             transmission.Text=""; 
             tiresize.Text ="";
              platlic.Text ="";
              renewal.Text ="";
              company.Text ="";
              
             premium.Text ="";
             due.Text = "";
             note.Text = "";


        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
            fillData();
        }

       private void fillData()
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand rrm;

            string sql = "select * from vehicleinfo";
            rrm = new SqlCommand(sql, mm);
            adapter.SelectCommand = rrm;
            adapter.Fill(dt);
            dataGridView1.DataSource = dt;
        }

       private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           DataView dv = new DataView(dt);
           if(comboBox1.SelectedItem.ToString()=="Show ALL")
           {
               dataGridView1.DataSource = dt;

           }
           else
           {
               dv.RowFilter = string.Format("status LIKE '%0%'", comboBox1.SelectedItem.ToString());

           }
       }
    }
}


What I have tried:

i want single record not duplicate
Posted
Updated 26-Sep-17 22:26pm

1. NEVER use string concatenation to create SQL query. You open your system to attacks called 'injection' - xkcd: Exploits of a Mom[^]
2. The only true - and right - way to prevent duplicate data is by declaring unique indexes in your database... SQL Server Tip : Preventing Duplicate Records Using the “Unique” Constraint | LGIT Smart Solutions[^]
 
Share this answer
 
Comments
Member 13421231 26-Sep-17 4:40am    
Violation of UNIQUE KEY constraint 'IX_vehicleinfo'. Cannot insert duplicate key in object
Delete the line and resolve the problem

C#
cmd.ExecuteNonQuery();



EASY SOLUTION
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900