Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8894333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:28:11+00:00 2026-06-14T23:28:11+00:00

My database is in SQL Server and I use Linq-to-SQL. I used from SP(Save

  • 0

My database is in SQL Server and I use Linq-to-SQL. I used from SP(Save cards) .
I put breakpoint in my code, when arrive at rdr = cmm.ExecuteReader(); get me exception!!!

 private void btnSave_Click(object sender, EventArgs e)
 {
            PersianCalendar jc = new PersianCalendar();
            string SaveDate = jc.GetYear(DateTime.Now).ToString();
            int from=Convert.ToInt32(txt_barcode_f.Text);
            int to=Convert.ToInt32(txt_barcode_t .Text);
            int quantity=Convert.ToInt32(to-from);
            int card_Type_ID=Convert.ToInt32(cmb_BracodeType .SelectedValue);
            int[] arrCardNum = new int[quantity];

            arrCardNum[0]=from;

             for (int i = from; i < to;i++ )
             {
                 for(int j=0; j<quantity ;j++)
                 {
                 arrCardNum[j]=from+j;
                 int r = arrCardNum[j];
                 sp.SaveCards(r, 2, card_Type_ID, SaveDate, 2);
                 }
             }
        }  

public void SaveCards(int Barcode_Num, int Card_Status_ID, int Card_Type_ID, string Save_Date, int Save_User_ID)
        {

                IDbCommand cmm;
                cmm = Linq.Connection.CreateCommand();
                try
                {           
                    cmm.Parameters.Add(new SqlParameter("@Barcode_Num", Barcode_Num));
                    cmm.Parameters.Add(new SqlParameter("@Card_Status_ID", 2));
                    cmm.Parameters.Add(new SqlParameter("@Card_Type_ID", Card_Type_ID));
                    cmm.Parameters.Add(new SqlParameter("@SaveDate", Save_Date));
                    cmm.Parameters.Add(new SqlParameter("@Save_User_ID", Save_User_ID));
                    cmm.CommandText = "SaveCards";
                    cmm.Connection.Open();
                    cmm.Connection = Linq.Connection;
                    cmm.CommandType = CommandType.StoredProcedure;
                    IDataReader rdr = null;

                    **rdr = cmm.ExecuteReader();**
                    while (rdr.Read())
                    {
                        Console.Write(" All Insert ! " + "\n");
                    }
                }
                catch (SqlException ex)
                {
                    SqlExceptionHandler(ex, Save_User_ID);
                }
                catch (Exception ex)
                {
                    PopularEexceptionHandler(ex, Save_User_ID);
                }
                finally
                { cmm.Connection.Close(); }

        }

when excute sp, show no result and display this:

when execute sp , display this:The INSERT statement conflicted with the CHECK constraint "CK_BarCode_Num". The conflict occurred in database "Parking", table "dbo.TBL_Cards", column 'BarCode_Num'. The statement has been terminated. No rows affected. (0 row(s) returned) @RETURN_VALUE = -6
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:28:13+00:00Added an answer on June 14, 2026 at 11:28 pm

    You’re a bit chaotic on how you set up your connection and command….. e.g. you open the connection before you even assign it! How is that going to work??

    My recommendation would be this order (based on the principle first do all the setup before opening the connection, and furthermore open the connection as late as possible, close it as quickly as possible) :

    IDbCommand cmm = Linq.Connection.CreateCommand();
    
    try
    {           
       // define name and type of command
       cmm.CommandText = "SaveCards";
       cmm.CommandType = CommandType.StoredProcedure;
    
       // assign connection
       cmm.Connection = Linq.Connection;
    
       // define parameters
       cmm.Parameters.Add(new SqlParameter("@Barcode_Num", Barcode_Num));
       cmm.Parameters.Add(new SqlParameter("@Card_Status_ID", 2));
       cmm.Parameters.Add(new SqlParameter("@Card_Type_ID", Card_Type_ID));
       cmm.Parameters.Add(new SqlParameter("@SaveDate", Save_Date));
       cmm.Parameters.Add(new SqlParameter("@Save_User_ID", Save_User_ID));
    
       // only now - after all the setup - open the connection, read the data
       cmm.Connection.Open();
    
       IDataReader rdr = rdr = cmm.ExecuteReader();
    
       while (rdr.Read())
       {
          ....
       }
    }
    

    To catch the errors that the execution throws, replace your catch block for the SqlException with this:

    catch (SqlException ex)
    {
       StringBuilder sbErrors = new StringBuilder();
    
       foreach (SqlError error in ex.Errors)
       {
          sbErrors.AppendLine(error.Message);
       }
    
       string allErrors = sbErrors.ToString();
    }
    

    and debug into that catch block – what is the allErrors string in the end??

    Update: after a chat session, we know finally know what the message in the SQL exception is:

    The INSERT statement conflicted with the CHECK constraint “CK_BarCode_Num”. The conflict occurred in database “Parking”, table “dbo.TBL_Cards”, column ‘BarCode_Num’.

    Now we’re trying to find out what that constraint is / does and why it gets violated….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have SQL Server database and would like to use LINQ to Entities and
I have a database in ms-sql-server express 2008 which is accessed with linq-to-sql from
I'm trying to use SQL Server Management Studio to edit database tables. But I
We use web deploy packages to create and deploy a SQL Server database, when
I Use a Sql Server Compact Edition Database File For Store the Data in
I can use the sql server management studio to open a sqlserver 2000 database,
Platform used : Microsoft Visual Studio 2010 (C#) Database : SQL Server 2008 R2
In Our project I have used LINQ to SQL for every kind of database
I'm building a WPF app that connects to a SQL Server database using LINQ
I have just had to use LINQ to SQL on an SQL Server 2000

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.