Hi guys

I'm a newbie, and can't figure out the problem with the following code. I get the error inthe line in Bold. Please help

private void Form1_Load(object sender, EventArgs e)
        {
            con = new SqlConnection();
            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\KAMESH\\Documents\\Visual Studio 2010\\Projects\\bankDBApp\\bankDBApp\\bankAcc.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            con.Open();
            MessageBox.Show("Connecting to SQL Database", "Connecting");
            ds = new DataSet();
            string sql = "SELECT * FROM AccountInfo";
            da = new SqlDataAdapter(sql, con);
            da.Fill(ds, "Accounts");
            [B]foreach (DataRow dr in ds.Tables["AccountInfo"].Rows)[/B] 
            {
            accountNumber = (string) dr["AccountNumber"];
            cbAccountNumber.Text = accountNumber;
            }

            acc.AccountNumber = int.Parse(accountNumber);
            cbAccountNumber.Items.Add(accountNumber);
            con.Close();

        }

Recommended Answers

All 5 Replies

Don't see a line in bold in there.

The foreach statement is tho one that gives me the error. Thanks

da.Fill(ds, "Accounts");
foreach (DataRow dr in ds.Tables["AccountInfo"].Rows)

This is taken from your code. You tell the SqlDataAdapter to fill the dataset with a table named "Accounts", then you (in the foreach) ask for the rows from the "AccountInfo" table. There is no AccountInfo table, it doesn't matter what your SQL says.

Thanks, feel really silly now. All fixed up, now for the next problem. hopefully I don't need to post another question here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.