0
我不確定,但我無法獲取要顯示在文本框中的數據。這是我迄今爲止編寫的代碼。任何幫助都會很棒。盒子裏沒有任何東西,但是做了測試,我得到了消息框。有什麼我不正確的?如果需要,我可以提供訪問文件。但是,它只是五個領域的數據。來自訪問db文件的數據
DataSet DataSet1; //use to put data in form
System.Data.OleDb.OleDbDataAdapter dataadapter;
private void Breed_Load(object sender, EventArgs e)
{
dbconnect = new System.Data.OleDb.OleDbConnection();//database connection variable
DataSet1 = new DataSet(); //variable to help get info from DB
dbconnect.ConnectionString = "PROVIDER= Microsoft.Jet.OLEDB.4.0; Data Source=C:/Pets.mdb"; //location of DB to open
dbconnect.Open(); //open command for DB
string sql = "SELECT * From tblPets"; //sql string to select all records from the table pets
dataadapter = new System.Data.OleDb.OleDbDataAdapter(sql, dbconnect); // pulls the records from sql command
MessageBox.Show("Database is Open");
dataadapter.Fill(DataSet1, "Pets"); // used the database to fill in the form.
NavRecords(); //calls NavRecords Method
dbconnect.Close();
MessageBox.Show("Database is Closed");
dbconnect.Dispose();
}
private void NavRecords()
{
DataRow DBrow = DataSet1.Tables["Pets"].Rows[0];
//PetNametextBox.Text = DBrow.ItemArray.GetValue(1).ToString(); //puts data in textbox
TypeofPettextBox.Text = DBrow.ItemArray.GetValue(1).ToString();//puts data in textbox
PetWeighttextBox.Text = DBrow.ItemArray.GetValue(2).ToString();//puts data in textbox
ShotsUpdatedtextBox.Text = DBrow.ItemArray.GetValue(3).ToString();//puts data in textbox
AdoptabletextBox.Text = DBrow.ItemArray.GetValue(4).ToString();//puts data in textbox
BreedtextBox.Text = DBrow.ItemArray.GetValue(5).ToString();//puts data in textbox
}
爲什麼不只是'DBrow [0]/DBrow [1] ...' – Rahul
設置一個斷點並檢查該行是否實際包含數據。考慮使用「DBrow [0] .ToString()」,你也應該考慮使用DataGridView。 – MrFox
我設置了一個斷點,沒有看到任何事情。但我不是100%在哪裏看。 UGH –