Solved

data reader... error

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

Author Closing Comment

by:Mike Eghtebas
ID: 39613842
Thank you.
0

Featured Post

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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