Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

752 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