Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

data reader... error

Posted on 2013-10-31
4
Medium Priority
?
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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 2000 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 34

Author Closing Comment

by:Mike Eghtebas
ID: 39613842
Thank you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

596 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