المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : ListView SelectedIndexChanged exception



C# Programming
05-06-2009, 04:24 PM
Hello. I have a listview where I load data from sql database. ListView has two columns (name, surname) where data is loaded. On the selectedindexchanged event, when an item from the listview is selected it gets the other data from the database, based on selection, and writes it to several textboxes... basicly a phonebook. If I select someone from the list the info is displayed in the txtboxes, but if i click smeone a second time i get an exception "NullRefferenceException" "Object reference not set to an instance of an object."



private void lvNameList_SelectedIndexChanged(object sender, EventArgs e)
{
ClearSearch();
select = "SELECT * FROM agenda_telefonica WHERE nume = '" + lvNameList.FocusedItem.Text + "' AND prenume = '" + lvNameList.FocusedItem.SubItems[1].Text + "'";
SqlDataReader dr = realSql.Select(select);
if (!dr.HasRows)
{
MessageBox.Show("Information not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
while (dr.Read())
{
tbName.Text = dr[1].ToString().Trim() + " " + dr[2].ToString().Trim();
tbPhoneNumber1.Text = dr[3].ToString().Trim();
tbPhoneNumber2.Text = dr[4].ToString().Trim();
tbPhoneNumber3.Text = dr[5].ToString().Trim();
tbFaxNumber.Text = dr[6].ToString().Trim();
tbEmail.Text = dr[7].ToString().Trim();
tbCompany.Text = dr[8].ToString().Trim();
tbService.Text = dr[9].ToString().Trim();
tbEquipment.Text = dr[10].ToString().Trim();
}
}
dr.Close();
realSql.Close();
}
}


The exception is raised the second time i click an item in the list and points to the select string. Does it have something to do with the FocusedItem?