Hello guys I am having a problem the read function of SQLDataReader is not returning anuthing. Please help me to figure out the problem.
I am passing data from this function which by the way is of deleteEntry class
string name = this.textBox1.Text;
string connstring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Gardezi\Documents\Visual Studio 2012\Projects\homeWork2\homeWork2\Database1.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(connstring);
string query = "Select * from diaryDB where Title=@name";
SqlCommand com = new SqlCommand(query, con);
SqlParameter p = new SqlParameter("name", name);
com.Parameters.Add(p);
con.Open();
SqlDataReader d = com.ExecuteReader();
deleteResult r = new deleteResult(d);
con.Close();
r.Show();
Then here in constructor of class deleteResult I am intializing the combo box.
public deleteResult(SqlDataReader d)
{
InitializeComponent();
while (d.Read())
{
this.comboBox1.Items.Add((d["title"] + "-" + d["description"]).ToString());
}
}
But when I try to get data from the DB it successfully aquires the data but the program never enters the loop where I am trying to read the data
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string name = this.comboBox1.SelectedItem.ToString();
string[] arr = name.Split('-');
string p1 = arr[1];
string connstring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Gardezi\Documents\Visual Studio 2012\Projects\homeWork2\homeWork2\Database1.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(connstring);
string query = "Select * from diaryDB where Description=@p1";
SqlCommand com = new SqlCommand(query, con);
SqlParameter p = new SqlParameter("p1", name);
com.Parameters.Add(p);
con.Open();
SqlDataReader d = com.ExecuteReader();
int p11 = d.FieldCount;
MessageBox.Show(""+p11);
while (d.Read())
{
this.textBox1.Text = d["Title"].ToString();
this.richTextBox1.Text = d["Description"].ToString();
this.pictureBox1.Image = Image.FromFile(d["pic"].ToString());
}
con.Close();
query = "delete from diaryDB where Description=@p1";
com = new SqlCommand(query, con);
p = new SqlParameter("p1", p1);
com.Parameters.Add(p);
con.Open();
com.ExecuteNonQuery();
con.Close();
}