[英]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.