Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to connect the data base for creating and backup data. when I try to connect the data base it gives me an error Invalid attempt to read when no data is present.

Please help me on this.

What I have tried:

 public partial class Backup_or_Restore_Data : Form
    {
        private SqlConnection con;
        private SqlCommand command;
        private SqlDataReader reader;
        string sql = "";
        string connectionString = "";
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                connectionString = "Data Source = " + textBox1.Text + "; User Id = " + textBox2.Text + "; Password = " + textBox3.Text + "";
                con = new SqlConnection(connectionString);
                con.Open();
                sql = "EXEC sp_databases";
                command = new SqlCommand(sql, con);
                reader = command.ExecuteReader();
                comboBox1.Items.Clear();
                while (reader.Read());
                {
                    comboBox1.Items.Add(reader[0].ToString());

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
Posted
Updated 17-Mar-20 2:41am
Comments
Richard MacCutchan 17-Mar-20 8:27am    
What does your stored procedure actually do? I suspect that it does not return a dataset.

1 solution

Look at your code:
C#
while (reader.Read());
{
    comboBox1.Items.Add(reader[0].ToString());
}
The semicolon at the end of the while line means that all the lines have been read before you enter the curly bracket block where you try to use it.

Remove the semicolon.
 
Share this answer
 

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