Sunday, 18 November 2012

There is a situation in drop down list where first item is "[Select]" and you need to make mandatory selection from drop down list except "[Select]".
Means, user must select value from drop down list but did not allow to select first element "[Select]". We can achieve this using required field validator control. For that we need to set "InitialValue" property.

Here is example for this.
In this example we take one product drop down list control in this list we list out all products, But the First Item is "[Select]" and it's value is "-1" in product drop downlist.
And We take one Required Field Validator control and set it's ValidateControl property to product drop down list id. We also set "InitialValue=-1" property.
We also took one button so that on click of button required field valdator control validate user input.
In this example we set InitialValue to -1 so that , if user select "[Select]" item from drop down list and click on "Save" button Validator control executed and validation message display on screen.
You can see that validation message in output.

Friday, 16 November 2012

You can get parent child hierarchy records or category sub category records or recursive records from table in sql server and display as a tree structure.
We are using A common table expression (CTE)  to get result. In fact we are using Recursive CTE to get this recursive data.

Here is example for this.
In this example we take one table "category_master". This table contains recursive records. This table has "category_id" , "category_name" and "parent_category_id" columns. "parent_category_id" column contain reference of "category_id". We can insert "N - Level" of parent child relationship records. We retrieve those records using recursive "CTE" and we can also do Hierarchy level by applying spacing so it's look like tree structure.

Wednesday, 7 November 2012

There is a situation where you want to put asp button in telerik Rad AjaxPanel and execute some javascript function using "onClientClick" event and base on that javascript function you need to execute server side code, i.e If javascript function return false then do nothing else if return true it will execute server side code. Here is solution for this.
Without do any trick this code do not work If you simply set "onClientClick" with javascript function without update panel this work but under Rad AjaxPanel this is not execute server side code after executing javascript function.

This Do not work :  onClientClick="return ConfirmDeleteMessage();"

This will Work :  OnClientClick="if (!ConfirmDeleteMessage('Are you sure you want to delete ? ')) { return false; }"

Here is example for this.
In this example we take one asp button and put this button inside Rad AjaxPanel.
And also we take on javascript function, in this function we take confirmation window and ask use for allow delete message.
On clicking on the "Ok" server side code is being executed.

ASPX Code : 
   <telerik:radscriptblock id="RadScriptBlock1" runat="server">
       <script type="text/javascript">
           function ConfirmDeleteMessage(strMessage) {
               if (window.confirm(strMessage)) {
                   return true;
               }
               else {
                   return false;
               }
           }
        </script>
    </telerik:radscriptblock>

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <telerik:radajaxpanel id="pnlMessage" runat="server">
        <div>
            <asp:Label runat="server" ID="lblMessage" ></asp:Label><br />
            <asp:Button runat="server" ID="btnDeleteItem" Text="Delete Item" 
                                     OnClientClick="if (!ConfirmDeleteMessage('Are you sure you want to delete ? ')) { return false; }"
                                     OnClick="btnDeleteItem_Click"  />
            <br /><br />
         </div>
         </telerik:radajaxpanel>
     </form>

C# Examples :
    protected void btnDeleteItem_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "Server side code executed.";
        
    }

VB.net Examples :
    Protected Sub btnDeleteItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        lblMessage.Text = "Server side code executed."
    End Sub

Output (After Click on "Delete Item" button confirmation dialog box open) :


Output (After click n "Ok" button of confirmation dialog box server side code of "Delete Item" button  executed.) : 


This article is very useful for .Net Beginners.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.




Thursday, 1 November 2012

You can insert value in identity column manually using INSERT statement using "IDENTITY_INSERT" property. You need to set IDENTITY_INSERT property to ON to allow identity insert value.
If there is already IDENTITY_INSERT property ON for particular table and again you set to ON It will raise error "states SET IDENTITY_INSERT is already ON and reports the table it is set ON".
After Set ON and Insert records we need to set OFF to avoid unnecessary errors.
You need permission to set this property. By default "sysadmin" server role and the "db_owner" and "db_ddladmin" database role and the object owner has permission.

Here is example for this.
In this example we take one database table "product_master". This table has one identity column "product_id" and other column "product_name".
Now we are inserting some records and we can see that it's identity column has serial unique number. After that we delete one records from middle of that records. So now one gap introduce in serial unique number. Now if you want to insert another product it did not generate that missing serial number it will simply generate greater value from existing numbers.
Suppose we have certain situations where we want to insert that particular identity number record. So at that time we can use "IDENTITY_INSERT" property. You can see in screen shot that there is product_id="5" record is deleted and is missing from list. Now we again insert that identity value using "IDENTITY_INSERT".

SQL Query :
SET IDENTITY_INSERT [product_master] ON
INSERT INTO product_master (product_id,product_name) values(5,'USB')
SET IDENTITY_INSERT [product_master] OFF

Output (Missing Record Id Sequence) : 


Output (After inserting Missing Record Id Sequence) :


This is the very useful SQL Server Scripts.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.