Click here to Skip to main content
15,902,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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.

C#
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

C#
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();
   }
Posted
Updated 2-Dec-14 4:47am
v2
Comments
ZurdoDev 2-Dec-14 10:50am    
What exactly is your question?
Syed Muhammad Ali Gardezi 2-Dec-14 10:54am    
In the 3rd function the read statement is not working.I can't understand why it is not working
ZurdoDev 2-Dec-14 10:57am    
What do you mean by 3rd function? The 3rd code block above? If so, that means no records were returned. You can check d.HasRows.
Tomas Takac 2-Dec-14 10:51am    
Did you try to debug it? Does the message box show up? What happens then? I mean after you close the message box? It has to continue to the next statement which is your loop.
Syed Muhammad Ali Gardezi 2-Dec-14 10:53am    
yes I debugged it but it never enters the loop. I can't understand why it is not entering the loop

1 solution

SqlParameter p = new SqlParameter("p1", name); -- should name be p1 ?


In case I was unclear...
SqlParameter p = new SqlParameter("p1", name p1);
 
Share this answer
 
v2
Comments
Syed Muhammad Ali Gardezi 2-Dec-14 11:17am    
Thanks for pointing it out yes the name should be p1
Maciej Los 2-Dec-14 11:19am    
Sure, it should be @p1 ;)
PIEBALDconsult 2-Dec-14 11:22am    
No, that's not the problem, the @ is understood. :D
Maciej Los 2-Dec-14 11:43am    
Not sure that @ is obvious ;(
PIEBALDconsult 2-Dec-14 11:46am    
It is... to the code. Maybe not to the developer or the next developer, but the code knows it should be there. At least for some ADO.net providers. I always supply the @ myself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900