Saturday, August 15, 2009

Meaning of Freedom


What is freedom?
Is it synonymous with the right to do anything?
Does this give us a right to articulate our feelings as we wish to do so? I’m really confused and hence trying to explore the meaning of the apparently simple word.
Then I have started asking people about their understanding of freedom?
A taxi driver answered that buying his own cab in one day is the freedom for him.
College girl told me that to buy as many as apparels is the definition of freedom for her.
A kid replied to me that if he doesn’t need to go to the school for one day, he’ll consider to be free!!!
One of my friends told me to spend a day uninterrupted in hill station is a freedom for her.
Another said to read the book that he loves through out the day defines the freedom for him.
And for me freedom means:
To write in my blog,
To sing a song or to play a guitar---
To paint on a canvas
To chat with my close friends for hour
To travel to some unknown place
To walk in the drizzle
And to reach at the zenith!!!”

Saturday, August 8, 2009

Confirmation box in winform with yes/ no

Recently I need to write confirmation box in winform and based on the selection of my user (whether user select "Yes" or "no") , some action will be performed.

Lets have a look at the code snippet

DialogResult dResTest;
dResTest = MessageBox.Show( "Do you want to add something ?", "Confirm Document Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dResTest == DialogResult.No)
{
Application.Exit();
}
else
{

//Calling some function if user clicks Yes
ViewExistingTicket
();

}

Just use the intelliscense feature, you'll find some other useful properties after giving dot to MessageBoxButtons and MessageBoxIcon

Thursday, August 6, 2009

Using output parameter of SQL Server stored procedure in C#

Recently I need to write a small piece of code for one small module that tracks different tasks of a group of production support worker.
In the first part of this post, I would like to write some part of the code to depict how to use output parameter in store proc. The name of the store proc is

Alter procedure sp_check_dupl_rec
(
@ticketID
nvarchar(80),
@ErrorFlag char(1) out,
@ErrorText nvarchar(4000) out

)
As
BEGIN
set @ErrorFlag
= 0
Select DISTINCT TicketId from tbl_TaskTracker where TicketId = @ticketID
IF @@ROWCOUNT > 0
begin
set @ErrorFlag =1
set @ErrorText ='Ticket that you are going to enter already exists.'

--print @ErrorText
RETURN
end
END

In the second part of the code , I like to write C# code that is showing how do we use output parameter of SQL SERVER stored proc in our C# code to get relevant information.

bool DuplicateData = false;
string strCon = null;
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader rdr = null;
try
{
strCon = System.Configuration.ConfigurationSettings.AppSettings["dbtest"];
conn = new SqlConnection(strCon);
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd = new SqlCommand("sp_check_dupl_rec", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter oParam = cmd.Parameters.AddWithValue("@ticketID", txtTicketNo.Text);
oParam = cmd.Parameters.AddWithValue("@ErrorFlag", 'a');
oParam.Direction = ParameterDirection.Output;
oParam = cmd.Parameters.AddWithValue("@ErrorText", '0');
oParam.Size = 4000;
oParam.Direction = ParameterDirection.Output;
rdr = cmd.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (conn != null)
conn.Close();
if (rdr != null)
rdr.Close();
}
int intErrCode = Convert.ToInt16(cmd.Parameters["@ErrorFlag"].Value);
if (intErrCode == 1)
{
string strErrTxt = Convert.ToString(cmd.Parameters["@ErrorText"].Value);
MessageBox.Show(strErrTxt);
DuplicateData = false;
}
else
DuplicateData = true;
return DuplicateData;
}

Please have a look specially at the highlighted part of the above code.
Kindly check prior to open the connection whether the connection has already been opened and don't forget to close the datareader after using it!

FEEDJIT Live Traffic Feed