ListView SelectedIndexChanged exception
-
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?
-
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?
-
Thanks. :)
-
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?
You are closing
realSql
which is not opened in the method you have posted. It could therefore berealSql
(null after closing) that is causing the exception.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
You are closing
realSql
which is not opened in the method you have posted. It could therefore berealSql
(null after closing) that is causing the exception.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
realSql is a dll refference to a sql class i've created. it has an select(string select) which open an sql connection then returns a datareader ("SqlDataReader dr = realSql.Select(select)"). So the realSql.Close() closes the connection opened by the Select() method.