Wednesday, June 10, 2009

Return value from stored procedure to c#

Suppose you have a stored procedure which returns a bit value 0 or 1. If you want to call that procedure from c# and get that bit value, do like this below:

let say, the stored procedure has:

Name: dbo.sp_dateCheck

Variables:
@laptop varchar(50),
@startdate DateTime,
@enddate DateTime

and return bit variable: @CondFlag

SqlCommand com = new SqlCommand("dbo.sp_dateCheck", conn);
com.CommandType = CommandType.StoredProcedure;

try
{
conn.Open();
com.Parameters.Add(new SqlParameter("@laptop", Laptop.Text));
com.Parameters.Add(new SqlParameter("@startdate", startDate. DateTime));
com.Parameters.Add(new SqlParameter("@enddate", endDate. DateTime));

//read the returned value from stored procedure
com.Parameters.Add("@CondFlag", SqlDbType.Bit);
com.Parameters["@CondFlag"].Direction = ParameterDirection.ReturnValue;
com.ExecuteNonQuery();
int ReturnedVal = (int)com.Parameters["@CondFlag"].Value;

if (ReturnedVal == 0)
{ // do this
}

else
{//do this
}
}

catch
{
}

Happy Programming!!!

Monday, May 25, 2009

Running a SQL query to replace certain characters in a table

Let say,
You have a database that contains many characters and symbols. If you want to replace those , just run the following SQL query:

(Here, suppose a symbol ~! need to replace by - then your query will be:)

UPDATE table
SET ColumnName= replace(LTRIM(RTRIM(ColumnName)), '~!', '-')

Happy Programming!:)

Sunday, May 10, 2009

Master page with Searchbox

Consider a master page with a search functionality and other number of content pages derived from that master page, so that searchbox appears in all pages. If you want to display a search results in Search.aspx page no matter where you are( in other content pages),add this code:

In MasterPage.cs:
Protected void search_button_Click(object sender, EventArgs e)
{
//redirect textbox's value to search.aspx page
Response.Redirect(@"~/search.aspx?q=" + Server.UrlEncode(txtSearch.Text));
}

In Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
//read the master page's redirected value
string str = Request.QueryString[@"q"];
}

--Happy Coding

Wednesday, April 15, 2009

Disable Browser Back Button

If you want to disable going back in the previous page, I am going to show you very easy method to do this:

Suppose you are in Page 1 and go to page 2. If you want to go back to page 1 , you are able to go. But now, if you want to disable that going back features, add the following script in the body tag of the page 1:

body onload=”if(history.length>0)history.go(+1);”

This works only for IE and not for Mozilla Firefox. For Mozilla do this:

add this script within head:

function noBack()
{
window.history.forward()
}
noBack();
window.onload=noBack;
window.onpageshow=function(evt)
{
if(evt.persisted)noBack()
}
window.onunload=function()
{
void(0)
}

It works not only with Firefox but also with IE, Safari, Opera.