0

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();

        }
3
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by tirthraj201186
0

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

0
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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.