Link to home
Start Free TrialLog in
Avatar of kvnsdr
kvnsdr

asked on

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

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();
            }            
ASKER CERTIFIED SOLUTION
Avatar of microbolt
microbolt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial