none
How to iterate and display all the rows in a DataTable? RRS feed

  • Question

  • Hello Friends,

    I have created a DataTable with StudentID as Primary key. I have set AutoIncrement property on that column with AutoIncrementSeed=1 and AutoIncrementStep=1. Other columns are FirstName and LastName. I tried to insert a row into the table with the overloaded Add Method:

    studentTable.Rows.Add(new object { "A", "B" });

    When I tried to insert this row there was an error saying that "A" cannot be inserted into StudentID column. I understand that I should give a value to StudentID column. But as it is set to be an AutoIncrementColumn there is no need to insert a value to that column. But still the compiler is complaining that i should provide a value for that column. What is the solution for this. Please can anybody help me out?

    And at runtime how can I accept values from the user and then populate the rows? I am using Disconnected objects. Just creating an in memory database. Please help me. If you know any websites where i can learn ADO.NET with examples can you please provide the link for the same.

    Please help. I am interested in taking Exam 70-516. So please help me with this.

    Thanking you Abhilash D K 

          ( MCA Student )

    Monday, July 30, 2012 5:34 AM

Answers

  • It's not the way to add new row. You can add a new ros like below,

    DataRow newRow = studentTable.Rows.Add();
    newRow[1] = "A";
    newRow[2] = "B";

    The above code will insert a new row {n, A, B} into student table.

    And here is a good ADO.NET tutorial,

    http://msdn.microsoft.com/en-us/data/aa937722.aspx

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Heslacher Monday, July 30, 2012 5:49 AM
    • Marked as answer by Mike FengModerator Thursday, August 9, 2012 2:46 PM
    Monday, July 30, 2012 5:40 AM

All replies

  • It's not the way to add new row. You can add a new ros like below,

    DataRow newRow = studentTable.Rows.Add();
    newRow[1] = "A";
    newRow[2] = "B";

    The above code will insert a new row {n, A, B} into student table.

    And here is a good ADO.NET tutorial,

    http://msdn.microsoft.com/en-us/data/aa937722.aspx

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Heslacher Monday, July 30, 2012 5:49 AM
    • Marked as answer by Mike FengModerator Thursday, August 9, 2012 2:46 PM
    Monday, July 30, 2012 5:40 AM
  • Hello,

    Thankyou for the reply. I will also check that link.

    Thanking you,

    Abhilash D K

    Tuesday, July 31, 2012 11:30 AM