"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

March 24, 2012

Tool Developer Notes - Part V

[Previous Post in Series - Tool Developer Notes - Part IV]

This post is based on work done in last couple of weeks

Tip #1 - While trying to run a Winforms Exe, I used to get below message prompt in my Win7 Machine

After a little google effort MSDN forum question was useful to fix this warning. Workaround is edit the manifest file in Debug folder which contains the Exe file

Added below lines of code

This fixed the message.

Tip #2 - C# File Exists Check

Tip #3 - C# Directory Exists Check

Tip #4 - C# - Check Return Value in SQL Command Return

In the earlier post on Tool Developer Notes Series I was not checking if the return value is NULL,
  • Modifying the example to check returned object is not null
  • Simple Windows Console Application
  • Add the references as per mentioned example code snippet
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
namespace TestExample
{
    class Program
    {
        public static void Main(string[] args)
        {

            Program P = new Program();
            P.FetchScalarValue();
        }

        public void FetchScalarValue()
        {
            try
            {
                Console.WriteLine("Fetch Single DB Values Example \n");
                SqlCommand comm = new SqlCommand();
                comm = SetUpConnection();
                OpenConnection(comm);
                comm.CommandType = CommandType.Text;
                comm.CommandText = @"SELECT Top 1 FirstName
                                  FROM [AdventureWorksLT2008R2].[SalesLT].[Customer]";
                object result;
                result = comm.ExecuteScalar();
                if(result!= null)
                {
                    Console.WriteLine("Query Result is " + result.ToString());
                }
                Console.ReadLine();
                Console.ReadLine();
            }
            catch (Exception EX)
            {

            }
        }
        public SqlCommand SetUpConnection()
        {
            string strConn = ConfigurationManager.AppSettings["ConnectionString"];
            SqlCommand comm = new SqlCommand();
            comm.Connection = new SqlConnection(
                strConn);
           return comm;
        }
        public void OpenConnection(SqlCommand comm)
        {
            comm.Connection.Open();
        }
        public void CloseConnection(SqlCommand comm)
        {
            comm.Connection.Close();
        }
    }
} 

App.config file Entry


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
      <appSettings file="" >
            <clear />
            <add key="ConnectionString" value="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorksLT2008R2;Data Source=.\SQLSERVER2008R2" />
      </appSettings>
</configuration>


Happy Learning!!!

No comments: