Solved

C# vs. Visual Studio .NET Coding Practices....????

Posted on 2004-08-16
1
188 Views
Last Modified: 2010-04-17
I feel the most confusing aspect in learning C# .NET Visual Studio is finding code on the Internet (below) that appears to be good C# coding, however I'm not sure how to use the 'automatic' drag-n-drop features to avoid coding the old way.

Q. How would the code snippet below look if coded using C# .NET Visual Studio???


private void PopAbsent()
            {
                  listBox2.Items.Clear();
                  //List Absent Reps
                  listBox2.BeginUpdate();
                  string v_tsql = "SELECT RTRIM([rep]) FROM Supportstats.dbo.absent_rep";
                  SqlConnection myConnection = new SqlConnection(v_sqlconnstring);
                  SqlCommand myCommand = new SqlCommand(v_tsql,myConnection);
                  try
                  {
                        myConnection.Open();
                        SqlDataReader myReader = myCommand.ExecuteReader();
                        while (myReader.Read())
                        {
                              string v_repname = myReader.GetString(0);
                              listBox2.Items.Add(v_repname);
                        }
                        myReader.Close();
                        myConnection.Close();
                  }
                  catch
                  {
                        MessageBox.Show("Unable establish SQL Connection.\nVarify settings and try again.", "SQL Connection Test: Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                  }
                  listBox2.EndUpdate();
            }            
0
Comment
Question by:kvnsdr
1 Comment
 
LVL 6

Accepted Solution

by:
microbolt earned 50 total points
ID: 11815003
Here is how C# autoformats it; however, formating is more of a personal preference.  You should be able to cut and pasted directly into it with no problems.  If your really into wanting to have your projects formated like C# does by default just hightlight a block of code and press CTRL+K or goto:  Edit -> Advanced -> Format Selection.


private void PopAbsent()
{
      listBox2.Items.Clear();
      //List Absent Reps
      listBox2.BeginUpdate();
      string v_tsql = "SELECT RTRIM([rep]) FROM Supportstats.dbo.absent_rep";
      SqlConnection myConnection = new SqlConnection(v_sqlconnstring);
      SqlCommand myCommand = new SqlCommand(v_tsql,myConnection);
      try
      {
            myConnection.Open();
            SqlDataReader myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {
                  string v_repname = myReader.GetString(0);
                  listBox2.Items.Add(v_repname);
            }
            myReader.Close();
            myConnection.Close();
      }
      catch
      {
            MessageBox.Show("Unable establish SQL Connection.\nVarify settings and try again.", "SQL Connection Test: Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
      }
      listBox2.EndUpdate();
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 58
What is Python programming? 3 68
White board coding practice 3 61
listing all functions in JavaScript 19 114
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

746 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

12 Experts available now in Live!

Get 1:1 Help Now