Solved

data reader... error

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

Expert Comment

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

Author Comment

by:Mike Eghtebas
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
Thank you.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

6 Experts available now in Live!

Get 1:1 Help Now