Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-16
1
Medium Priority
?
201 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 150 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …
Simple Linear Regression

782 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