locked
insert operation not working RRS feed

  • Question

  • well, this , to me sounds wired,  but, I can not see any data after performing the insert operation. the worst part is, when i try to insert the SAME data again, it throws up the following error message !!!which means, the data is there somewhere, but not in the table !!!

     

    please help me out, this seems so strange to me ! 

     

     

    System.Data.SqlClient.SqlException was unhandled
    
      Message=Violation of PRIMARY KEY constraint 'PK_client_client'. Cannot insert duplicate key in object 'dbo.client_client'.
    The statement has been terminated.
    
      Source=.Net SqlClient Data Provider
    
      ErrorCode=-2146232060
    
      Class=14
    
      LineNumber=1
    
      Number=2627
    
      Procedure=""
    
      Server=\\.\pipe\066FE0F2-0949-4A\tsql\query
    
      State=1
    
      StackTrace:
    
           at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
    
           at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
    
           at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
    
           at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
    
           at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
    
           at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
    
           at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
    
           at ClientKing.ClientKing.button4_Click(Object sender, EventArgs e) in D:\Data\Study Material\Aarsh\GaryInsuranceAndTax\Application\ClientKing\ClientKing\ClientKing.cs:line 84
    
           at System.Windows.Forms.Control.OnClick(EventArgs e)
    
           at System.Windows.Forms.Button.OnClick(EventArgs e)
    
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    
           at System.Windows.Forms.Control.WndProc(Message& m)
    
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    
           at System.Windows.Forms.Button.WndProc(Message& m)
    
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
    
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    
           at System.Windows.Forms.Application.Run(Form mainForm)
    
           at ClientKing.Program.Main() in D:\Data\Study Material\Aarsh\GaryInsuranceAndTax\Application\ClientKing\ClientKing\Program.cs:line 17
    
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
    
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    
           at System.Threading.ThreadHelper.ThreadStart()
    
      InnerException: 
    
    
    
    


    Tuesday, January 10, 2012 5:39 AM

Answers

  • It means, data is already added. Open your output folder /bin/debug/ or bin/Release. You will find your database file there (if you are usign local database file). If you find database file there, that is the file you are working on. Just open the file, and you will find the inserted rows.
    Please mark this post as answer if it solved your problem. Happy Programming!
    Tuesday, January 10, 2012 6:29 AM
  • Hi Ablaze,
    Welcome to the MSDN forum!

    How is it going with the problem based on Adavesh's suggestion?

    According to the message, it seems there is a duplicate value in the primary key column. You mentioned that you insert the same  data into the database, however, as far as I know, same value for primary key is not allowed. If you still want to insert the same data, I suggest you not insert the primary-key column, and instead set that column as int and auto increase. Just set the Identity value in the properties of the column in the design view.  Or perhaps you can look at this link: http://msdn.microsoft.com/en-us/library/bb726011.aspx 

    Meanwhile, since this is a question more related to SQL Server part, you can also try some SQL Server forums, for example, Getting started with SQL Server.
    http://social.msdn.microsoft.com/Forums/en-NZ/sqlgetstarted/threads

    Have a nice day!

    yoyo

     


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 12, 2012 3:10 AM
    Moderator

All replies

  • here is the code that inserts the data into the table :

     

     

    String insertCommand = "INSERT INTO [client_client] ([client_id], [client_firstName], [client_lastName],[client_address1], [client_address2], [client_city], [client_state], [client_zip], [client_secondaryNo], [client_additionalNo], [client_fax], [client_email], [client_birthdate], [client_ein], [client_ssn], [client_dlNo], [client_policyType], [client_carrier], [client_effectiveDate], [client_renewalDate],[client_totalPremium],[client_percentComission],[client_earnedComission], [client_lastWishYr], [client_account], [client_remarks]) VALUES ('" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox9.Text + "','" + textBox8.Text + "','" + textBox11.Text + "', '" + textBox10.Text + "', '" + textBox12.Text + "', '" + textBox14.Text + "','" + textBox13.Text + "','" + textBox16.Text + "','" + textBox17.Text + "','" + dateTimePicker1.Value.ToString() + "','" + textBox19.Text + "', '" + textBox18.Text + "','" + textBox21.Text + "','" + textBox24.Text + "', '" + textBox25.Text + "', '" + dateTimePicker2.Value.ToString() + "', '" + dateTimePicker3.Value.ToString() + "', '" + textBox15.Text + "','" + textBox20.Text + "','" + textBox22.Text + "', '" + (DateTime.Now.Year - 1).ToString() + "', '" + account + "', '" + richTextBox1.Text + "')";
    
    ----------------------------------------------------------------------------------------------------------------------------------------------------
    and here is the snippet that actually executes the query ...
    ----------------------------------------------------------------------------------------------------------------------------------------------------
    SqlCommand SQLCommand_AddClient = new SqlCommand(insertCommand, SQLConn_AddClient);
    SQLCommand_AddClient.ExecuteNonQuery();
    



     




    Tuesday, January 10, 2012 5:43 AM
  • Hi, 

    you can try like this....

     private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True;");
                try
                {
                    SqlCommand cmd = new SqlCommand("INSERT INTO customers(firstName,lastName,address,city) VALUES ('wee','weeee','dfddd', 'weee')", conn);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Connection Succeful");
                }
                catch (SqlException ex)
                {
     
                    MessageBox.Show("There is an Error" + ex);
                }
                finally
                {
                    conn.Close();
                    MessageBox.Show("Connection Closed");
                }
            }
    



    Don't forget to mark the post as answer or vote as helpful if it does, Regards - Rajasekhar.R
    Tuesday, January 10, 2012 6:15 AM
  • It means, data is already added. Open your output folder /bin/debug/ or bin/Release. You will find your database file there (if you are usign local database file). If you find database file there, that is the file you are working on. Just open the file, and you will find the inserted rows.
    Please mark this post as answer if it solved your problem. Happy Programming!
    Tuesday, January 10, 2012 6:29 AM
  • Just a quick note:

     

    You can use using statements instead of adding a finally block to close the connection:

     

    using (SqlConnection conn = new SqlConnection)
    {
       try
       {
          //Code here
       }
       catch
       {
          //Code here
       }
    }
    



    Be sure to up-vote me if I helped you. :) E-mail me if you need some extensive help. Contact: matija.cupic.97@gmail.com
    Tuesday, January 10, 2012 11:20 AM
  • Hi Ablaze,
    Welcome to the MSDN forum!

    How is it going with the problem based on Adavesh's suggestion?

    According to the message, it seems there is a duplicate value in the primary key column. You mentioned that you insert the same  data into the database, however, as far as I know, same value for primary key is not allowed. If you still want to insert the same data, I suggest you not insert the primary-key column, and instead set that column as int and auto increase. Just set the Identity value in the properties of the column in the design view.  Or perhaps you can look at this link: http://msdn.microsoft.com/en-us/library/bb726011.aspx 

    Meanwhile, since this is a question more related to SQL Server part, you can also try some SQL Server forums, for example, Getting started with SQL Server.
    http://social.msdn.microsoft.com/Forums/en-NZ/sqlgetstarted/threads

    Have a nice day!

    yoyo

     


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 12, 2012 3:10 AM
    Moderator