Solved

data reader... error

Posted on 2013-10-31
4
162 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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