使用单独的方法添加到datatable会产生错误(vs2013)

时间:2018-03-05 12:49:20

标签: c# visual-studio-2013

我正在制作一个可以在数据表中添加和编辑书籍的库程序来执行此操作我试图将初始化和添加到数据表中的方法不同但我似乎无法在此处工作是我的代码:

 class Book
{
    static DataTable libary = new DataTable();
    public static void Datatype()
    {
        //DataColumn Column;
        DataColumn ISBN = new DataColumn();
        //ISBN
        //library.Columns.Add("ISBN");
        ISBN.ColumnName = "ISBN";
        ISBN.DataType = typeof(int);
        ISBN.Unique = true;
        ISBN.AllowDBNull = false;
        ISBN.Caption = "ISBN";
        ISBN.DefaultValue = 00;
        libary.Columns.Add(ISBN);
        //Title
        DataColumn Title = new DataColumn();
        Title.ColumnName = "Title";
        Title.DataType = typeof(string);
        Title.Unique = false;
        Title.AllowDBNull = false;
        Title.Caption = "Book Tile";
        Title.DefaultValue = "--";
        libary.Columns.Add(Title);
        //Author
        DataColumn Author = new DataColumn();
        Author.ColumnName = "Author";
        Author.DataType = typeof(string);
        Author.Unique = false;
        Author.AllowDBNull = false;
        Author.Caption = "Author name";
        Author.DefaultValue = "--";
        libary.Columns.Add(Author);
        //Genre
        DataColumn Genre = new DataColumn();
        Genre.ColumnName = "Genre";
        Genre.DataType = typeof(string);
        Genre.Unique = false;
        Genre.AllowDBNull = false;
        Genre.Caption = "Book Genre";
        Genre.DefaultValue = "--";
        libary.Columns.Add(Genre);
        // OnLoan
        DataColumn OnLoan = new DataColumn();
        OnLoan.ColumnName = "OnLoan";
        OnLoan.Unique = false;
        OnLoan.DataType = typeof(Boolean);
        OnLoan.AllowDBNull = false;
        OnLoan.DefaultValue = false;
        OnLoan.Caption = "check to see if book is on loan";
        libary.Columns.Add(OnLoan);

    }
   /*internal static void add(DataTable libery)
    {

            DataRow Row;
            Row = libery.NewRow();
            //askes user for ISBN number and then adds to datatable
            int ISBN = Convert.ToInt32(Console.ReadLine());
            Row["ISBN"] = ISBN;
            // askes user for book title 
            string Title = Console.ReadLine();
            Row["Title"] = Title;
            DataRow row = libery.Rows[0];
            Console.WriteLine(row["ISBN"]);
            Console.ReadKey();

    }*/

    internal static void add(DataTable libary)
   {
       Console.Clear();
       Console.WriteLine("input ISBN number:");
       //DataTable libary = new DataTable();
       DataRow Row;
       Row = libary.NewRow();
       //askes user for ISBN number and then adds to datatable
       string ISBN = Console.ReadLine();
       Int16 ISBN_con = Convert.ToInt16(ISBN);            
       Row["ISBN"] = ISBN_con;
       libary.ImportRow(Row);
       // askes user for book title 
       string Title = Console.ReadLine();
       Row["Title"] = Title;
       libary.ImportRow(Row);
       DataRow row = libary.Rows[0];
       Console.WriteLine(row["ISBN"]);
       Console.ReadKey();
   }
   internal static void view(DataTable libary)
    {
        Console.Clear();
        foreach (DataColumn column in libary.Columns)
        {
            Console.Write("Coloum: ");
            Console.Write(column.ColumnName);
            Console.Write(" ");
            Console.WriteLine("\n");
        }
        Console.ReadLine();
    }
}

以下是错误消息: 类型为' System.ArgumentException'的未处理异常发生在System.Data.dll中  其他信息:专栏' ISBN'不属于表

我不确定我做错了什么

0 个答案:

没有答案