系统无效的转换异常

时间:2014-01-04 15:28:15

标签: c# sql sqlconnection

我遇到了C#和sql连接问题。当谈到C#我是一个noobie并且几乎不知道我在做什么:/我正在尝试按照教程解释如何一步一步地做所有事情并且由于某种原因它不起作用当我尝试在我的数据库和应用程序上执行此操作。

这是我的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);

1 个答案:

答案 0 :(得分:0)

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