Solved

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

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

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.

749 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