繁体   English   中英

系统无效的强制转换异常

[英]System invalid cast exception

我在使用C#和sql连接时遇到问题。 当涉及到C#时,我是一个菜鸟,几乎不知道我在做什么:/我正在尝试遵循一个教程,该教程解释了如何逐步进行所有操作,由于某种原因,当我无法使用时尝试在我的数据库和应用程序上执行此操作。

这是我的Form1.cs

using System;
using System.Collections;
using System.Windows.Forms;

namespace Praca_Inzynierska
{
public partial class Form1 : Form
{
    private Connection sqlCon = new Connection();
    private ArrayList list = new ArrayList();
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'restaurantDataSet2.Employees' table. You can move, or remove it, as needed.
        this.employeesTableAdapter.Fill(this.restaurantDataSet2.Employees);

         FillTextFieldsEmployees(1);

    }
    public void FillTextFieldsEmployees(int EmployeeID)
    {
        list = sqlCon.GetAllEmployees(EmployeeID);

        textFirstName.Text = list[0].ToString();
        textLastName.Text = list[1].ToString();
        textAdress.Text = list[2].ToString();
        textCity.Text = list[3].ToString();
        textPhoneNumber.Text = list[4].ToString();
        textEmail.Text = list[5].ToString();
        textBirthDate.Text = list[6].ToString();
        textAge.Text = list[7].ToString();
        textGender.Text = list[8].ToString();
        textTitle.Text = list[9].ToString();
        textSalary.Text = list[10].ToString();
    }

    private void dataGridViewEmployees_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        var currentRowIndex = dataGridViewEmployees.SelectedCells[0].RowIndex;
        int currentIndex = (int) dataGridViewEmployees.Rows[currentRowIndex].Cells[0].Value;
        FillTextFieldsEmployees(currentIndex);
    }
}
}

这是我班的Connection

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;


namespace Praca_Inzynierska
{
  public class Connection
  {
   private String connectionString = "Data Source = MAKSKOMP\\SQL2012EXP; Initial      Catalog = Restaurant;Integrated Security = True";

   public ArrayList GetAllEmployees(int EmployeeID)
   {
       using (var connection = new SqlConnection(connectionString))
       {
           connection.Open();
           String query = "SELECT *  FROM Employees WHERE EmployeeID = '" + EmployeeID +"'";
           using (var command = new SqlCommand(query, connection))
           {
               var reader = command.ExecuteReader();
               var list = new ArrayList();
               while (reader.Read())
               {
                   String FirstName = reader.GetString(1);
                   String LastName = reader.GetString(2);
                   String Adress = reader.GetString(3);
                   String City = reader.GetString(4);
                   String PhoneNumber = reader.GetString(5);
                   String Email = reader.GetString(6);
                   DateTime BirthDate = reader.GetDateTime(7);
                   Int16 Age = reader.GetInt16(8);
                   String Gender = reader.GetString(9);
                   String Title = reader.GetString(10);
                   int Salary = reader.GetInt32(11);

                   list.Add(FirstName);
                   list.Add(LastName);
                   list.Add(Adress);
                   list.Add(City);
                   list.Add(PhoneNumber);
                   list.Add(Email);
                   list.Add(BirthDate);
                   list.Add(Age);
                   list.Add(Gender);
                   list.Add(Title);
                   list.Add(Salary);

               }
               connection.Close();
               reader.Close();
               return list;
           }

       }
   }
}

}

由于某种原因,它在这个特定的地方破裂

  int currentIndex = (int) dataGridViewEmployees.Rows[currentRowIndex].Cells[0].Value;

我试图一步一步地调试它,也没有完成此循环

 while (reader.Read())
           {
               String FirstName = reader.GetString(1);
               String LastName = reader.GetString(2);
               String Adress = reader.GetString(3);
               String City = reader.GetString(4);
               String PhoneNumber = reader.GetString(5);
               String Email = reader.GetString(6);
               DateTime BirthDate = reader.GetDateTime(7);
               Int16 Age = reader.GetInt16(8);
               String Gender = reader.GetString(9);
               String Title = reader.GetString(10);
               int Salary = reader.GetInt32(11);

               list.Add(FirstName);
               list.Add(LastName);
               list.Add(Adress);
               list.Add(City);
               list.Add(PhoneNumber);
               list.Add(Email);
               list.Add(BirthDate);
               list.Add(Age);
               list.Add(Gender);
               list.Add(Title);
               list.Add(Salary);

           }

它结束于

Int16 Age = reader.GetInt16(8);

由于您的代码已使用数据绑定网格,因此请尝试将TextBox控件也绑定到相同的绑定源。 当数据已经加载到网格中时,从数据库中获取数据没有多大意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM