Tuesday, March 24, 2009

Get the Value of a Grid Cell

protected void dgCustomer_EditCommand(object source, DataGridCommandEventArgs e)
{
string CustomerId = e.Item.Cells[0].Text;
}

Thursday, March 19, 2009

Code Behind of RDLC Reporting

ReportViewer1.Visible = true;
SqlConnection thisconn = new SqlConnection(conn);
System.Data.DataSet thisDataSet = new System.Data.DataSet();
SearchValue[0] = new SqlParameter("@CategoryID",tbxCustomerId.Text);
SqlCommand cmd=new SqlCommand();
CustomerDatasetTableAdapters.CustomerTableAdapter daCustomer=new CustomerTableAdapter();
ReportDataSource dataSource = new ReportDataSource("CustomerDataset_Customer",thisDataSet.Tables["Customer"]);
dataSource.Value = daCustomer.GetDataBy(Convert.ToInt32(tbxCustomerId.Text));
ReportDataSource("DataSetProducts_ShowProductByCategory",thisDataSet.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(dataSource);
ReportViewer1.LocalReport.Refresh();

Wednesday, March 18, 2009

Update Query with where Clause in a seperate Class

Create a method of Update in a seperate Class

public bool Update(string customerId, string customerName, string customerPhone, string customerAddress, string customerAge)
{
SqlCommand objCommand = new SqlCommand();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ConnectionString);
SqlParameter objParameter = new SqlParameter();
try
{
string strSql = "UPDATE Customer "+
"SET CustomerName = @CustomerName, CustomerPhone = @CustomerPhone, CustomerAddress = @CustomerAddress, "+
"CustomerAge = @CustomerAge WHERE CustomerID = @CustomerID";
objCommand.CommandText = strSql;
objCommand.Connection = conn;
objParameter = new SqlParameter("@CustomerID", customerId);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@CustomerName", customerName);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@CustomerPhone", customerPhone);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@CustomerAddress", customerAddress);
objCommand.Parameters.Add(objParameter);
objParameter = new SqlParameter("@CustomerAge", customerAge);
objCommand.Parameters.Add(objParameter);
objCommand.Connection.Open();
return (objCommand.ExecuteNonQuery() > 0 ? true : false);
}
catch (Exception ex)
{
conn.Close();
return false;
}
finally
{
conn.Close();
}
}

Create a object of Update on the update button click event which get CustomerId through Query String


int customerid = Convert.ToInt32(Request.QueryString["CustomerId"].ToString());
Customer objCustomer = new Customer();
objCustomer.Update(customerid.ToString(), tbxCustomerName.Text.Trim(), tbxCustomerPhone.Text.Trim(), tbxCustomerAddress.Text.Trim(), tbxCustomerAge.Text.Trim());

Dataset with where Clause

On A button Click event we get all the Values of Customer Table(CustomerName, CustomerAddress, CustomerAge, CustomerPhone) through Customer ID which is present in the Query String.

protected void btnGet_Click(object sender, EventArgs e)
{
int customerid = Convert.ToInt32(Request.QueryString["CustomerId"].ToString());
SqlParameter objParameter = new SqlParameter();
SqlCommand objCommand = new SqlCommand();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT CustomerID, CustomerName, CustomerPhone, CustomerAddress, CustomerAge " +
"FROM Customer where CustomerID=@CustomerID", conn);
da.SelectCommand.Parameters.Add(new SqlParameter("@CustomerID", customerid));
da.SelectCommand.Parameters["@CustomerID"].Value = customerid;
DataSet ds = new DataSet();
da.Fill(ds, "Customer");
foreach (DataRow pRow in ds.Tables["Customer"].Rows)
{
tbxCustomerName.Text = pRow["CustomerName"].ToString();
tbxCustomerPhone.Text = pRow["CustomerPhone"].ToString();
tbxCustomerAddress.Text = pRow["CustomerAddress"].ToString();
tbxCustomerAge.Text = pRow["CustomerAge"].ToString();
}
}

Monday, March 9, 2009

Linq with Classes using Join in Query

class Name
    {
        public string name { get; set; }
        public string EmailAddress { get; set; }
        public string Phone { get; set; }
        public Name(string n,string e,string p)
        {
            name = n;
            EmailAddress = e;
            Phone = p;
        }
    }

class EmailAddress
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public EmailAddress(string n, string e)
        {
            Name = n;
            Address = e;
        }
    }

class Temp
    {
        public string Address { get; set; }
        public string Phone { get; set; }
        public Temp(string a, string p)
        {
            Address = a;
            Phone = p;
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            EmailAddress[] addrs ={
                                     new EmailAddress("Sheeban","Sheeban@quntech.com"),
                                     new EmailAddress("Ahmed","Ahmed@quntech.com"),
                                     new EmailAddress("Memon","Memon@quntech.com")
                                 };
            Name[] name ={
                            new Name("Sheeban","Sheeban@quntech.com","555-555-555"),
                            new Name("Ahmed","Ahmed@quntech.com","111-111-111"),
                            new Name("Memon","Memon@quntech.com","777-777-777")
                        };
            var eAddrs = from entry in addrs
                         join detail in name
                         on entry.Name equals detail.name
                         select new Temp(entry.Address,detail.Phone);
            foreach (Temp t in eAddrs)
                Console.WriteLine("{0}\t{1}",t.Address,t.Phone);
            Console.ReadLine();
        }
    }

HTTP Modules Events

Every HTTP Module must implement following two methods of IHTTPModule interface:

InitTo register/initialize event handler to the events of HTTP Module for HTTP based application.

DisposeTo perform a clean up code means resource releasing, object removing from memory and such other resources releasing which used explicitly.

Following are list of events with their brief description:

BeginRequest: Event fired whenever any asp.net based request sent to web server. If you need to do perform at the beginning of a request for example, modify show banners, log HTTP Header information, Get/Set cultures, Response.Filter to generate response for browser according to your need.

AuthenticateRequest: If you want to check authentication of request that request comes from authenticated user or not means wants to implement custom authentication scheme. For example, look up a requested user credentials against a database to validate.

AuthorizeRequest: This method is used specifically to implement authorization mechanisms means authenticated user/request has what privileges/rights/access in that specific application for example, either user has access on all pages or not of that website or has write to create file or not or visit report pages and like this.

ResolveRequestCache: This event determines if a page served from the Output cache. If you want to write your own caching module (for example, build a file-based cache rather than a memory cache), synchronize this event to determine whether to serve the page from the cache.

AcquireRequestState: Session state is retrieved from the state store. If you want to build your own state management module, synchronize this event to grab the Session state from your state store.

PreRequestHandlerExecute: This event occurs before the HTTP handler is executed.

PostRequestHandlerExecute: This event occurs after the HTTP handler is executed.

ReleaseRequestState: Session state is stored back in the state store. If you are building a custom session state module, you must store your state back in your state store.

UpdateRequestCache: This event writes from output back to the Output cache. If you are building a custom cache module, you have to write the output back to your cache.

Error: this event always occurs when any exception (unhandled error occurs in application, this event specifically uses to handle or log error messages of that web application. (Heavily used in Error Logging Modules and Handlers (ELMAH) kind of applications). You can learn about ELMAH more from following link in detail:http://dotnetslackers.com/articles/aspnet/ErrorLoggingModulesAndHandlers.aspx

EndRequest: Request has been completed. You may want to build a debugging module that gathers information throughout the request and then writes the information on the page.

By above events list you must be getting wonder about difference between Global.asax as somehow events of Global.asax are pretty same, so let me tell you difference between Global.asax and HTTP Module (another common question asked in interviews)

But you need to register/initialize these Events explicitly in “Init” method; following is sample code of IHTTPModule implementation for couple of events.