Skip to content
Loading
  • Mageshwaran R
    +2
    Try this,
     
    1. SqlCommand cmd = new SqlCommand("INSERT INTO Monitor(LCDSrNo,ProductName,LCDProductNo,Date)  
    2. SELECT LCDSrNo=@LCDSrNo,ProductName=@ProductName,LCDProductNo=@LCDProductNo,Date=@Date", con);  
    3. cmd.CommandType = CommandType.Text;  
    4. cmd.Parameters.AddWithValue("@LCDSrNo", textBox2.Text);  
    5. cmd.Parameters.AddWithValue("@ProductName", textBox3.Text);  
    6. cmd.Parameters.AddWithValue("@LCDProductNo", textBox4.Text);  
    7. cmd.Parameters.AddWithValue("@Date", dateTimePicker1);  
    8. con.Open();  
    9. cmd.ExecuteNonQuery();  
    10. con.Close();  
     
    +2
  • Sachin Singh
    +2
    1. CREATE TABLE Monitor(  
    2.  ID int IDENTITY(1, 1) PRIMARY KEY,  
    3.  Name varchar(50)  
    4.   
    5.  ) 
    +2
  • Sachin Singh
    +2
    @suhel
    still your id is not auto incremented
    1. right click on Id column (key symbol) and go to properties 
    2. under properties double click on Identity specification
    +2
  • Mageshwaran R
    +2
    Hi suhel,
    Id is not identity column so you pass the id value.
    1. SqlCommand cmd = new SqlCommand("INSERT INTO Monitor(Id,LCDSrNo,ProductName,LCDProductNo,Date)    
    2. SELECT Id=@Id,LCDSrNo=@LCDSrNo,ProductName=@ProductName,LCDProductNo=@LCDProductNo,Date=@Date", con);    
    3. cmd.CommandType = CommandType.Text;  
    4. cmd.Parameters.AddWithValue("@Id", 1);  
    5. cmd.Parameters.AddWithValue("@LCDSrNo", textBox2.Text);    
    6. cmd.Parameters.AddWithValue("@ProductName", textBox3.Text);    
    7. cmd.Parameters.AddWithValue("@LCDProductNo", textBox4.Text);    
    8. cmd.Parameters.AddWithValue("@Date", dateTimePicker1);    
    9. con.Open();    
    10. cmd.ExecuteNonQuery();    
    11. con.Close();
    +2