Solved

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

Posted on 2004-08-16
1
194 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
recursion example 16 141
Please help| Getting the syntax error in below query. 2 65
What does != "" mean in programming 8 82
Programming Codes 2 22
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about problems I had with the new location API and permissions in Marshmallow
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 …

831 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