tôi gặp sự cố trong đó tôi chỉ có thể truy xuất dữ liệu từ cơ sở dữ liệu trên một hộp văn bản. Những gì tôi muốn, tôi muốn lấy dữ liệu của tất cả các textbox từ cơ sở dữ liệu, nhưng khi tôi đã cố gắng, nhận được lỗi:Không thể chuyển đổi từ chuỗi thành int32
Đây là mã:
string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb;Persist Security Info=False;");
private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxQuantityContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxDescContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxSubTotalContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxTotalContainer = new List<List<TextBox>>();
private void Form1_Load(object sender, EventArgs e)
{
UpdateTextPosition();
OleDbDataReader dReader;
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT [Code] FROM [Data]", conn);
dReader = cmd.ExecuteReader();
AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection();
while (dReader.Read())
{
string numString = dReader[0].ToString().PadLeft(4, '0');
codesCollection.Add(numString);
}
dReader.Close();
conn.Close();
}
private void UpdateDatas()
{
OleDbDataReader dReader;
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT [Description], [Price] FROM [Data] WHERE [Code][email protected]", conn);
cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][0].Text;
dReader = cmd.ExecuteReader();
while (dReader.Read())
{
this.textBoxDescContainer[0][0].Text = dReader["Description"].ToString();
this.textBoxSubTotalContainer[0][0].Text = dReader["Price"].ToString();
}
dReader.Close();
conn.Close();
}
Tôi đã thử add:
cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][1].Text;
và
while (dReader.Read())
{
this.textBoxDescContainer[0][1].Text = dReader["Description"].ToString();
this.textBoxSubTotalContainer[0][1].Text = dReader["Price"].ToString();
}
nhưng tôi đã nhận một lỗi mà nói: "Không thể chuyển đổi giá trị tham số từ một strin g để một int32"
Định dạng mã không đúng. –
bạn gặp lỗi ở đâu? – Default
Từ các đề xuất không thành công, có vẻ như bạn có vấn đề về dữ liệu. Hiển thị cho chúng tôi dữ liệu mẫu? Nó có thể là Mã hoặc Giá chứa số nguyên không. – cjb110