Wednesday, November 9, 2011

How to Completely Disable Viewstate in ASP.NET

I tried to disable Viewstate of a website by set EnableViewState Page directive to false.
But when I get the Page HTML Source I found the following line:

 <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
 value="/wEPDwUKMTY1NDU2MTA1MmRk7eWz8DBE7zF7MW8sS4wNndKCr5gBJPZZ/LOS1KIbBo4=" /> 

  protected override void SavePageStateToPersistenceMedium(object viewState)
  {
  }

  protected override object LoadPageStateFromPersistenceMedium()
  {
    return null;
  }

Tuesday, November 1, 2011

Difference between Inner and Outer Join in SQL

Joins are used to combine the data from two tables, with the result being a new, temporary table.
The temporary table is created based on column(s) that the two tables share, which represent meaningful column(s) of comparison.
The goal is to extract meaningful data from the resulting temporary table.
Joins are performed based on something called a predicate, which specifies the condition to use in order to perform a join.
A join can be either an inner join or an outer join, depending on how one wants the resulting table to look.

Example:
Suppose you have two Tables, with a single column and data as follows:

Table1: ID1                        Table2: ID2
            A                                        C 
            B                                        D
            C                                        E
            D                                        F

Inner Join Query would look like:

select * from Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID2

You would get the following result:

ID1    ID2
C       C  
D       D

Left Outer Join Query would look like:

select * from Table1 LEFT OUTER JOIN Table2 ON Table1.ID1 = Table2.ID2

You would get the following result:

ID1    ID2
A       null 
B       null
C        C
D        D

Full Outer Join Query would look like:

select * from Table1 FULL OUTER JOIN Table2 ON Table1.ID1 = Table2.ID2

You would get the following result:

 ID1    ID2
 A       null 
 B       null
 C        C
 D        D
null      E
null      F

Wednesday, October 19, 2011

How to Copy some rows from a GridView to another in ASP.NET

A junior developer asked me can I select some rows from a GridView and copy them to another GridView?
I told him sure all you have to do is to create a DataTable and the columns that will be added to it.
Now you will loop for each row in the first GridView then add the selected rows to the DataTable.
Set the DataSource of the other GirdView to DataTable.
Here is the code that would do the trick:

DataTable dt = new DataTable();
DataRow dr;
       
dt.Columns.Add(new DataColumn("FirstName"));
dt.Columns.Add(new DataColumn("LastName"));
      
foreach (GridViewRow row in GridViewEmployees.Rows)
 {            
  if(((CheckBox)row.Cells[3].FindControl("CheckBox")).Checked == true)
   {
    dr = dt.NewRow();
    dr["ColumnName1"] = ((Label)row.Cells[0].FindControl("Label")).Text;
    dr["ColumnName2"] = ((Label)row.Cells[1].FindControl("Label")).Text;
    dt.Rows.Add(dr);
   }
 }
 
GridViewManagers.DataSource = dt;
GridViewManagers.DataBind();

Monday, October 3, 2011

Two Databound Fields in Gridview column

Boundfields are great in the Gridview, but they do have their limitations.
One is that it only has room for one bound field. So, what do you do when you want two or data fields.
For example, First and Last names returned to the same column.
Turn the BoundField into a TemplateField, with a label in it:

<asp:templatefield>
 <itemtemplate>               
  <asp:label id="lblname" runat="server" text="<%# Eval("Lname") +, "" + Eval("Fname") %>" />            
 </itemtemplate>
</asp:templatefield>

Thursday, September 1, 2011

Select Drop Down List Item After Declarative Databinding

The new declarative controls of ASP.NET 2.0 are really great. However, there are some times, when it may not seem so.
Let's say you have a DropDownList which is bound by a DataSource control. Also, you have other pages hitting this page, with querystrings, and based on the item in the querystring, you'd like the to select that item.
In ASP.NET 1.1, you'd just code it, and select it after the code, or in the Pre-render event.

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(Request.Querystring("YourQuerystring"));

Saturday, January 1, 2011

How to check SQL Server Database is Exists using C#

The following method using sys. schema and views such as sys.databases and sys.tables.
As we know that the old style sysobjects and sysdatabases and those catalog views have been deprecated since SQL Server 2005.

private static bool CheckDatabaseExists(string connectionString, string databaseName)
{
    string sqlQuery;
    bool result = false;

    try
    {
        SqlConnection conn = new SqlConnection(connectionString);
        sqlQuery = string.Format("SELECT database_id FROM sys.databases WHERE Name = '{0}'", databaseName);

        using (conn)
        {
            using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
            {
                conn.Open();
                int databaseID = (int)cmd.ExecuteScalar();
                conn.Close();
                result = (databaseID > 0);
            }
        }
    }
    catch (Exception)
    {
        result = false;
    }
    return result;
}

Hire Me

Follow me on Facebook

Follow me

Do you find this Blog helpful?

Follow by Email

About Me

My Photo
Expert Senior Software Developer

Microsoft Business Card