none
The Table Does Not Have Primary Key RRS feed

  • Question

  •  

    It is an ADO .NET situation with C# code around it.

     

    Trying to do a DataTable.Rows.Find (object[]) operation I am getting the above error.

     

    I swear, the table DOES have a primary key. It is one field, first column. I set it up in VFPro 9 MS myself, actually by two methods with the table browser==>modify and command window with a command. I see the key mark clearly displayed against the column in question. In some tables there were inadvertently duplicate rows and the wizard did not allowed me to set the primary key up. I removed those.

     

    Also in the Visual Studio 2005 I have all the OLEDB Databases attached with connection strings, etc. I can see columns and can see all the values if I wish. There is NO key sign agains this column in  there. It is not indicated that it is an index, it looks like a reqular column, HOWEVER, when I rightClick properties on the column I see that "Nullable = false" in the property window. This is an indication, I think that it is keyed, isn't it?

     

    Please, help.

     

    Thanks.

    Monday, September 17, 2007 5:28 PM

Answers

  •  AlexBB wrote:

    No, your answer does not help at all. You missed the question completely. Sorry. I hope you will reread it. I am asking why am I getting that error message. The uniqueness of the column is not being recognized by my source code at runtime when the table is a part of the dataset.

    Thanks.


    If i don't have your source, how i will know why you have that problem. Error message is clear and it's because your DataTable(not the table in your database) don't have primary key.

    If you don't have typed dataset, where in the table you will manually set a column to be primary key then how you think that DataTable will know that you have key in your database. So one way is to have typed dataset, and second way if you don't want to have typed dataset is to set the existance of the key using code.
    How to do it using code? Part of the answer for this question is in my previous post. That is how a column is marked as primary key. Answer is if column is Unique and have UniqueConstraint. Part missing is that UniqueConstraint have a parameter which marks the constraint as primary key. Here is the code for marking one column as primary key:
    YourTable.Constraints.Add(new System.Data.UniqueConstraint("Constraint1", new System.Data.DataColumn[] {YourTable.yourColumn}, true));   //true --> isPrimaryKey
    YourTable.yourColumn.AllowDBNull = false;
    YourTable.yourColumn.Unique = true;


    YourTable is name of DataTable object.
    yourColumn is column of your DataTable. You can replace it with YourTable.Columns[0] if key column is first column in the table.

    One simple way to check if datatable have primary key or any other constraint is to check Constraints collection. If it's empty then you can be sure that there is no key for the table.

    Monday, September 17, 2007 8:51 PM

All replies

  • Indication that a column is primary key is Unique property. When you select a column from DataTable in DataSet designer and make it PrimaryKey, automatically column is marked as Unique, AllowDBNull is false, new Unique constraint is created, and FindByColumnName method is created (which uses Rows.Find(primarycolumnvalue)). I hope this info will help you.
    Monday, September 17, 2007 7:33 PM
  •  boban.s wrote:
    Indication that a column is primary key is Unique property. When you select a column from DataTable in DataSet designer and make it PrimaryKey, automatically column is marked as Unique, AllowDBNull is false, new Unique constraint is created, and FindByColumnName method is created (which uses Rows.Find(primarycolumnvalue)). I hope this info will help you.

     

    Hi boban.s,

     

    You've got three stars in front of your name. I think it entails you to read the posts more carefullySmile

     

    No, your answer does not help at all. You missed the question completely. Sorry. I hope you will reread it. I am asking why am I getting that error message. The uniqueness of the column is not being recognized by my source code at runtime when the table is a part of the dataset.

     

    Thanks.

    Monday, September 17, 2007 8:31 PM
  •  AlexBB wrote:

    No, your answer does not help at all. You missed the question completely. Sorry. I hope you will reread it. I am asking why am I getting that error message. The uniqueness of the column is not being recognized by my source code at runtime when the table is a part of the dataset.

    Thanks.


    If i don't have your source, how i will know why you have that problem. Error message is clear and it's because your DataTable(not the table in your database) don't have primary key.

    If you don't have typed dataset, where in the table you will manually set a column to be primary key then how you think that DataTable will know that you have key in your database. So one way is to have typed dataset, and second way if you don't want to have typed dataset is to set the existance of the key using code.
    How to do it using code? Part of the answer for this question is in my previous post. That is how a column is marked as primary key. Answer is if column is Unique and have UniqueConstraint. Part missing is that UniqueConstraint have a parameter which marks the constraint as primary key. Here is the code for marking one column as primary key:
    YourTable.Constraints.Add(new System.Data.UniqueConstraint("Constraint1", new System.Data.DataColumn[] {YourTable.yourColumn}, true));   //true --> isPrimaryKey
    YourTable.yourColumn.AllowDBNull = false;
    YourTable.yourColumn.Unique = true;


    YourTable is name of DataTable object.
    yourColumn is column of your DataTable. You can replace it with YourTable.Columns[0] if key column is first column in the table.

    One simple way to check if datatable have primary key or any other constraint is to check Constraints collection. If it's empty then you can be sure that there is no key for the table.

    Monday, September 17, 2007 8:51 PM
  •  boban.s wrote:
     AlexBB wrote:

    No, your answer does not help at all. You missed the question completely. Sorry. I hope you will reread it. I am asking why am I getting that error message. The uniqueness of the column is not being recognized by my source code at runtime when the table is a part of the dataset.

    Thanks.

    One simple way to check if datatable have primary key or any other constraint is to check Constraints collection. If it's empty then you can be sure that there is no key for the table.

     

    Thanks much. It is already meaningful. I think I can make sense of it tomorrow but now I am at a saturation point cognitively speaking, My wife just informed me that I should show up in 10 min and this is what I am gonna do.

     

    Thanks again.

    Monday, September 17, 2007 9:27 PM
  •  boban.s wrote:
     AlexBB wrote:

    No, your answer does not help at all. You missed the question completely. Sorry. I hope you will reread it. I am asking why am I getting that error message. The uniqueness of the column is not being recognized by my source code at runtime when the table is a part of the dataset.

    Thanks.


    If i don't have your source,

     

     

    You code does work. Thanks.

     

    Tuesday, September 18, 2007 5:59 PM