Solved

data reader... error

Posted on 2013-10-31
4
157 Views
Last Modified: 2013-10-31
this code works when line 19 is removed
            string cs = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
            SqlConnection con = new SqlConnection(cs);
            try
            {
                SqlCommand cmd1 = new SqlCommand("SELECT *  FROM Customers", con);
                con.Open();
                SqlDataReader rdr = cmd1.ExecuteReader();
                BindingSource source = new BindingSource();
                DataTable table = new DataTable();
                table.Columns.Add("CustomerID");
                table.Columns.Add("PostalCode");
                while (rdr.Read())
                {
                    DataRow  dataRow = table.NewRow();
                    string ozip = rdr["PostalCode"].ToString();
                    string nzip = ozip + "AAA";
                    dataRow["CustomerID"] = rdr["CustomerID"];
                    dataRow["PostalCode"] = ozip;
                    //dataRow["newzip"] = nzip;
                    table.Rows.Add(dataRow);
                }
                dataGridView2.DataSource = table;
            }
            catch
            {

            }

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39613803
What is the error message? What is the type of nzip?
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 39613807
You are very fast, here are the questions:

Q1: What is the typical syntax for Catch block to trap all errors?
Q2: How can I modify line 19 to add this new column to the grid view?

            catch
            {

            }
            finally
            {
                con.Close();
            }

Thank you.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39613830
What is the typical syntax for Catch block to trap all errors?
Well, to catch *all* errors, you would do something like:

try
{

}
catch (Exception ex)
{
    // do something with exception
}

Open in new window


But it's generally advised not to do the above. Exception is a base type, and all other exceptions inherit from it. To someone reading your code, catching the general Exception looks like you don't have any clue as to what exceptions can occur within your code, so you just threw in the general Exception as a shortcut.

How can I modify line 19 to add this new column to the grid view?

Try adding the following between lines 11 and 12:

table.Columns.Add("newzip");

Open in new window

0
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
ID: 39613842
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now