locked
constraint Exeption RRS feed

  • Question

  • Hello. I'm trying to save data from a list box but when I go to save information, I get the Unhadled constraint exeption. I've tried the if statement to see if it would help and it makes no difference. IWhat am I doing wrong? Here's the code

    param = new SqlParameter("@Track", SqlDbType.VarChar, 70);
          if(lstTracks.Text.ToString() != null)
          param.Value = lstTracks.Text;
          command.Parameters.Add(param);
    
    Any help would be greatly appreciated.
    PCRider
    Wednesday, February 16, 2011 1:47 AM

Answers

  • I figured it out. Just needed to change a value from this

     param = new SqlParameter("@Track", SqlDbType.NChar, 10);
    to this
     param = new SqlParameter("@Track", SqlDbType.VarChar, 70);
    Took care of the problem. Thanks for your help. I may need it again



    PCRider
    • Marked as answer by PCRider Thursday, February 17, 2011 1:23 AM
    Thursday, February 17, 2011 1:23 AM

All replies

  • Can you please post a bigger code snippet? This one seems OK to me so far, I need to see your insert command and table's definition.
    Premature optimization is the root of all evil in programming. (c) by Donald Knuth

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    Wednesday, February 16, 2011 4:33 AM
    Answerer
  • OK. Naomi Lets start with this error from my Tracks dataset:

     this.tracksTableAdapter.Fill(this.ensemblecsDataSet.Tracks);

    "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."

    Next is the Insert command

     public int InsertTracks(SqlConnection connection, int AlbumID)
        {
          SqlParameter param = null;
          SqlCommand command = new SqlCommand();
          int TrackID = -1;
    
          command.Connection = connection;
          command.CommandText = "INSERT INTO [Tracks] ([AlbumID], [Track], [Track#], [NumberOfTracks], [GuestArtist], [TrackLength]) VALUES " +
             " (@AlbumID, @Track, @Track#, @NumberOfTracks, @GuestArtist, @TrackLength ); " +
             " Select SCOPE_IDENTITY() as SCOPE_IDENTITY; ";
    
          command.CommandType = CommandType.Text;
    
          param = new SqlParameter("@AlbumID", SqlDbType.Int);
          param.Value = AlbumID;
          command.Parameters.Add(param);
    
          param = new SqlParameter("@Track", SqlDbType.NChar, 10);
          param.Value = DBNull.Value;
          command.Parameters.Add(param);
    
          param = new SqlParameter("@Track#", SqlDbType.Char, 2);
          param.Value = DBNull.Value;
          command.Parameters.Add(param);
    
          param = new SqlParameter("@NumberOfTracks", SqlDbType.Char, 3);
          param.Value = DBNull.Value;
          command.Parameters.Add(param);
    
          param = new SqlParameter("@GuestArtist", SqlDbType.VarChar, 60);
          param.Value = DBNull.Value;
          command.Parameters.Add(param);
    
          param = new SqlParameter("@TrackLength", SqlDbType.VarChar, 50);
          param.Value = txtTrackLength.Text.Trim();
          command.Parameters.Add(param);
    
          TrackID = Convert.ToInt32(command.ExecuteScalar());
    
          return TrackID;
    
        }
    
    

    Now for the table definition

    Tracks Table: Nulls

    TrackID      Int primary Key Not Allow

    AlbumID Int Foreign Key Allow

    Track VarChar(70) Allow

    NumberOfTrack  VarChar(3) Allow

    GuestArtist VarChar(60) Allow

    TrackLength datetime Allow

     

    Ok. you have it. Anything else, let me know


    PCRider
    Thursday, February 17, 2011 12:44 AM
  • I figured it out. Just needed to change a value from this

     param = new SqlParameter("@Track", SqlDbType.NChar, 10);
    to this
     param = new SqlParameter("@Track", SqlDbType.VarChar, 70);
    Took care of the problem. Thanks for your help. I may need it again



    PCRider
    • Marked as answer by PCRider Thursday, February 17, 2011 1:23 AM
    Thursday, February 17, 2011 1:23 AM