none
How to Create Autogenerate ID in C# ?

    Question

  • I have 1 textbox i wanna to make my id column from my table Art to autogenerate +1 evrytime by checking whats is curently last id on table ..

    here is my curently code :

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlServerCe;
    
    namespace SmartDeviceProject1
    {
        public partial class artadd : Form
        {
            public artadd()
            {
                InitializeComponent();
            }
            SqlCeConnection cn = new SqlCeConnection("Data Source=\\Program Files\\SmartDeviceProject1\\Ege.sdf");
            SqlCeCommand cmd;
    
            private void artadd_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'egeDataSet.Sub_Gr' table. You can move, or remove it, as needed.
                this.sub_GrTableAdapter.Fill(this.egeDataSet.Sub_Gr);
                // TODO: This line of code loads data into the 'egeDataSet.Group' table. You can move, or remove it, as needed.
                this.groupTableAdapter.Fill(this.egeDataSet.Group);
                // TODO: This line of code loads data into the 'egeDataSet.MR' table. You can move, or remove it, as needed.
                this.mRTableAdapter.Fill(this.egeDataSet.MR);
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                cn.Open();
                cmd = new SqlCeCommand("INSERT INTO Art(id,name,buy_price,sell_price,mr,group_id,subgroup_id) VALUES(@id,@name,@buy_price,@sell_price,@mr,@group_id,@subgroup_id)", cn);
                cmd.Parameters.Add("@id", textBox1.Text);
                cmd.Parameters.AddWithValue("@name", textBox2.Text);
                cmd.Parameters.AddWithValue("@buy_price", textBox3.Text);
                cmd.Parameters.AddWithValue("@sell_price", textBox4.Text);
                cmd.Parameters.AddWithValue("@mr", comboBox1.Text.Trim());
                cmd.Parameters.AddWithValue("@group_id", comboBox2.Text.Trim());
                cmd.Parameters.AddWithValue("@subgroup_id", comboBox3.Text.Trim());
    
                try
                {
                    int affectedRows = cmd.ExecuteNonQuery();
                    if (affectedRows > 0)
                    {
                        MessageBox.Show("Insert Successfull");
                        cn.Close();
                    }
                    else
                    {
                        MessageBox.Show("Insert Failed");
                        cn.Close();
                    }
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
    
                }
            }
    
        }
    }

    Sunday, December 09, 2012 10:36 PM

Answers

All replies