Wednesday, 16 May 2012

RDLC Basics

Basic Example Of  RDLC  (Report Definition Language Client-side)










To Understand From The Basic Let Start From Here..

            We have two tables

              1. MasterProduct

Brand
Product
ID
Brand1
Product1
101
Brand2
Product1
102
Brand3
Product2
103
Brand1
Product2
104
Brand4
Product3
105
Brand3
Product4
106
                  *ID is primary key here




       2. Transaction            
State
Quantity
ID
State1
10
101
State2
20
103
State3
40
104
State1
60
102
State2
40
101
State4
80
106
                         *Here ID is foreign key reference to ID of MasterProduct Table



Now Question is:

We want the below given output where State, Product1,2,3,4 are columns if we add product then column added and if we add state then it added to state column. It takes the quantity from Transaction and show in the rows accordingly.

And If You Have Opportunity to 

Use any of the data control of asp.net to show the output...I Suggest Use RDLC(report control of .net)

OUTPUT Will be:
State
Product1
Product2
Product3
Product4
State1
10+60=70



State2
40
20


State3

40


State4



80






use namespace...
using Microsoft.Reporting.WebForms;


Use function() Like

  public void BindReport()
    {
        SqlCommand cmd = new SqlCommand("spTest", con);
        cmd.CommandType = CommandType.StoredProcedure;
        if (con.State == ConnectionState.Closed)
            con.Open();

        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(ds);

        ReportDataSource rds = new ReportDataSource("DataSet1_spTest", ds.Tables[0]);
      
        ReportViewer1.LocalReport.DataSources.Clear();

        ReportViewer1.LocalReport.DataSources.Add(rds);
      
        ReportViewer1.LocalReport.Refresh();
    }






But How to add change RDLC at RUN Time??

How To Set Default Button for ENTER key Pressed Event, And How To Set Focus On A Particular Control

Default Enter Key Action To Avoid PostBack In ASP.Net
This is a default property that when one presses Enter key in the textbox or any control  there is a postback. So many people asked on forums
1. How to disable enter key in textbox?Or in any particular control
2. How to prevent postback or form submission when enter key is pressed in textbox in ASP.Net?
3)How to make click event fire of a particular button control when enter key is pressed.


1)What if you want to  prevent postback or form submission when enter key is pressed
 




 by JAVASCRIPT

<script type="text/javascript">
        function DisableEnterKey()
{
    if (window.event.keyCode == 13)
    {
        event.returnValue=false;
        event.cancel = true;
    }
}</script>
 
<body onkeydown="return DisableEnterKey()">
</body>
 
 
 




2) If you want default action of a button click on Enter Press


 
 
 <form id="form1"  runat="server" defaultbutton="Button1">
    <div>
    </div>
</form>
 
 
 
 
3) If you want default action of a button click on Enter Press and default focus on a textbox
 
 
 
 
 
 <form id="form1"  runat="server" defaultbutton="Button1" defaultfocus="TextBox1">
    <div>
    </div>
</form>
 
 
 
 
 
4)To Prevent Enter Press in particular text box in your from 
  
<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (event.keyCode!=13)"></asp:TextBox>
 
 
From code behind
 
TextBox1.Attributes.Add("onkeydown", "return (event.keyCode!=13);"); 



 
 
 


Thursday, 10 May 2012

Date Diffrence in SQL

Diffrence Between Dates In SQL

We use  convert(varchar, getdate(), 101) format to save data.which will save record in format mm/dd/yyyy  so that we can work on dates easily


On Insertion
Insert into tbSample values ('anycontent',convert(varchar, @OnDate, 101))




On Execution Time
SELECT    * from tbSample where OnDate between convert(varchar, '5/9/2012', 101) and convert(varchar, '5/11/2012', 101)";




Other DateTime Formats Are Below

SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM (or PM)

                                        -- Oct  2 2008 11:01AM         

SELECT convert(varchar, getdate(), 101) -- mm/dd/yyyy - 10/02/2008                 

SELECT convert(varchar, getdate(), 102) -- yyyy.mm.dd -- 2008.10.02          

SELECT convert(varchar, getdate(), 103) -- dd/mm/yyyy

SELECT convert(varchar, getdate(), 104) -- dd.mm.yyyy

SELECT convert(varchar, getdate(), 105) -- dd-mm-yyyy

SELECT convert(varchar, getdate(), 106) -- dd mon yyyy

SELECT convert(varchar, getdate(), 107) -- mon dd, yyyy

SELECT convert(varchar, getdate(), 108) -- hh:mm:ss

SELECT convert(varchar, getdate(), 109) -- mon dd yyyy hh:mm:ss:mmmAM (or PM)

                                        -- Oct  2 2008 11:02:44:013AM  

SELECT convert(varchar, getdate(), 110) -- mm-dd-yyyy

SELECT convert(varchar, getdate(), 111) -- yyyy/mm/dd

SELECT convert(varchar, getdate(), 112) -- yyyymmdd

SELECT convert(varchar, getdate(), 113) -- dd mon yyyy hh:mm:ss:mmm

                                        -- 02 Oct 2008 11:02:07:577    

SELECT convert(varchar, getdate(), 114) -- hh:mm:ss:mmm(24h)

SELECT convert(varchar, getdate(), 120) -- yyyy-mm-dd hh:mm:ss(24h)

SELECT convert(varchar, getdate(), 121) -- yyyy-mm-dd hh:mm:ss.mmm

SELECT convert(varchar, getdate(), 126) -- yyyy-mm-ddThh:mm:ss.mmm

                                        -- 2008-10-02T10:52:47.513

-- SQL create different date styles with t-sql string functions



SELECT convert(varchar(7), getdate(), 126)                 -- yyyy-mm

SELECT right(convert(varchar, getdate(), 106), 8)

Wednesday, 9 May 2012

Ajax Calender Extender Control

Ajax Calender Extender Control
     
Ajax Calender Extender Control: Its a Light weight 
control and works fine!




For that you have to drag  Ajax's ToolkitScriptManager and required a TextBox and Ajax's CalendarExtender

Below is the css for CalendarExtender,and the Textbox where we need to control CalendarExtender


  <style type="text/css">
 .cal_Theme1 .ajax__calendar_container   {
        background-color: #e2e2e2; 
        border:solid 1px #cccccc;
    }
    
    .cal_Theme1 .ajax__calendar_header  {
        background-color: #ffffff; 
        margin-bottom: 4px;
    }
    
    .cal_Theme1 .ajax__calendar_title,
    .cal_Theme1 .ajax__calendar_next,
    .cal_Theme1 .ajax__calendar_prev    {
        color: #004080; 
        padding-top: 3px;
    }
    
    .cal_Theme1 .ajax__calendar_body    {
        background-color: #e9e9e9; 
        border: solid 1px #cccccc;
    }
    
    .cal_Theme1 .ajax__calendar_dayname {
        text-align:center; 
        font-weight:bold; 
        margin-bottom: 4px; 
        margin-top: 2px;
    }
    
    .cal_Theme1 .ajax__calendar_day {
        text-align:center;
    }
    
    .cal_Theme1 .ajax__calendar_hover .ajax__calendar_day,
    .cal_Theme1 .ajax__calendar_hover .ajax__calendar_month,
    .cal_Theme1 .ajax__calendar_hover .ajax__calendar_year,
    .cal_Theme1 .ajax__calendar_active  {
        color: #004080; 
        font-weight: bold; 
        background-color: #ffffff;
    }
    
    .cal_Theme1 .ajax__calendar_today   {
        font-weight:bold;
    }
    
    .cal_Theme1 .ajax__calendar_other,
    .cal_Theme1 .ajax__calendar_hover .ajax__calendar_today,
    .cal_Theme1 .ajax__calendar_hover .ajax__calendar_title {
        color: #bbbbbb;
    }
                            </style>
                    <br />


                    <asp:TextBox ID="TextBoxDate" runat="server" ></asp:TextBox>
                     <asp:CalendarExtender 
    CssClass="cal_Theme1" 
    ID="CalendarExtender1" 
    runat="server" 
    PopupButtonID="Image1"
    PopupPosition="Right"
    TargetControlID="TextBoxDate">
                       </asp:CalendarExtender>


You may also like

 Best css style for autocomplete.
 Autocomplete Extender Control.
Ajax Progress Bar.







Create Datatable,Insert/Delete Row In Data Table With Grid View

Create Datatable,Insert/Delete Row In Datatable and Finaly Save Record In SQL Server Table
Here we'll discuss about how to create Datatable(by creating coloumns and then filling rows),how to delete row from temporary created Datatable,and Finally how to fetch Datatabledata into a SQL Server table


Now Lets figure out what we need to do
1)Instantiate DataTable 
2)Create Colomns
3)Insert  Rows
4)Insert Rows with Previous saved Rows
5)Delete Rows
6)Finally save all Rows in SQL table



Now Lets discuss these points bit broadly
1)As we discussed above, here we are instantiating DataTable

                                               DataTable TempDTable=new DataTable();

2)Now creating/add columns in DataTable

                                          TempDTable.Columns.Add("ColumnName", typeof(string));


3)Insert Rows

                            DataRow row=new  TempDTable.NewRow();//Instantiating DataRow for inserting row  in particular columns

  
                               row["ColumnName"] = TextBox1.Text;
4)Insert Rows with Previous saved Rows

        if (ViewState["PagedataTable"] == null) //if there is any row in datatable
        {           
            //Add coloum for your DataTable
        }
        else(else if exist then just fill up rows)
        {
            TempDTable = (DataTable)ViewState["PagedataTable"];//store previous rows saved in viewstate to Datatable
        }
        DataRow row =new  TempDTable.NewRow();//Instantiating DataRow for inserting row in particular columns
     row["ColumnName"] =TextBox1.Text;//fill row for perticular column
       


                  if (ViewState["PagedataTable"] == null) //if there is any row in datatable (if not then create if exist then just fill up rows)
        {           
            //below code will create coloum for your DataTable
            TempDTable.Columns.Add("Type", typeof(string));
            TempDTable.Columns.Add("Group", typeof(string));
            TempDTable.Columns.Add("Name", typeof(string));
            TempDTable.Columns.Add("GroupID", typeof(string));
            TempDTable.Columns.Add("NameID", typeof(string));
            TempDTable.Columns.Add("Particular", typeof(string));
            TempDTable.Columns.Add("ChequeNo", typeof(string));
            TempDTable.Columns.Add("Credit", typeof(float));
            TempDTable.Columns.Add("Debit", typeof(float));
          
        }
        else
        {
            TempDTable = (DataTable)ViewState["PagedataTable"];
        }
        DataRow row;//Instantiating DataRow for inserting row in particular columns

        row = TempDTable.NewRow();

        row["Type"] = LabelType.Text;
        row["Group"] = DropDownListGroup.SelectedItem.Text;
        row["Name"] = DropDownListName.SelectedItem.Text;
        row["GroupID"] = DropDownListGroup.SelectedValue;
        row["NameID"] = DropDownListName.SelectedValue;
        row["Particular"] = TextBoxPerticular.Text;
        row["ChequeNo"] = TextBoxChequeNo.Text;
        row["Credit"] = TextBoxCredit.Text;
        row["Debit"] = TextBoxDebit.Text;


        TempDTable.Rows.Add(row);//Fetching All Row in DataTable
        ViewState["PagedataTable"] = TempDTable;//After filling rows,we are storing DataTable in  View State so that we insert new value with  previous stored value in viewstate
        this.GridView1.Visible = true;

        GridView2.DataSource = TempDTable;//Bind it with a grid view where you want to store datatable  values
        GridView2.DataBind();
        ButtonSaveRecord.Visible = true;

5)To Check whether a coloumn entry alreay Existed with your Entered Value

 if (ViewState["tempProductDatatable"] != null)
        {
            TempDTable= (DataTable)ViewState["PagedataTable"];
            for (int i = 0; i < TempDTable.Rows.Count; i++)
            {
                if (TextBoxITEMCODE.Text == TempDTable.Rows[i]["Item_Code"].ToString())
                {
                    LblCode.Text = "xx";
                }
            }
        }
        if (LblCode.Text == "xx")
        {
                // means value already existed in Item_Code coloumn
          }
        else
        {
                // means value does not existed in Item_Code coloumn
         }







6) Delete Rows
between creating and fetching record from datatable to SQL server table you may also like (you must know) how to delete a record/row if its needs to be deleted!!

So here is a below code which will fired on RowDeleting Command of grid view!!


 protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int s = e.RowIndex;
        DataTable TempDtForDel = (DataTable)ViewState["PagedataTable"];
        TempDtForDel.Rows[s].Delete();
        GridView2.DataSource = TempDtForDel;
        GridView2.DataBind();
    }
So we have Done!!we've created a datatable which will further fillup rows as per button click event

7) Finally save all Rows in SQL table:
Now thing comes later is save that data (in datatable in SQL Server table) so here we go...
a)Here we are first creating a table to save final value from data table...

CREATE TABLE [dbo].[tb_Sample]
(
    [Voucher_Id] [int] IDENTITY(1,1) NOT NULL,
    [VoucherType] [varchar](50) NULL,
    [OfGroup] [varchar](100) NULL,
    [Acc_Name] [varchar](100) NULL,
    [Particulars] [varchar](100) NULL,
    [ChequeNo] [varchar](60) NULL,
    [Debit] [float] NULL,
    [Credit] [float] NULL
)

b)Now below is a command to save datatable records/rows in SQL server table

        con.Open();
        DataTable dtTemp = new DataTable();
        dtTemp = (DataTable)ViewState["PagedataTable"];

        for (int i = 0; i < dtTemp.Rows.Count; i++)
        {
            SqlCommand cmd2 = new SqlCommand("insert into tb_Sample values (@VoucherType,@Group,@Acc_Name, @Particulars,@ChequeNo,@Debit,@Credit)", con);


            cmd2.Parameters.Add("@VoucherType", SqlDbType.VarChar).Value = dtTemp.Rows[i]["Type"].ToString();
            cmd2.Parameters.Add("@Group", SqlDbType.VarChar).Value = dtTemp.Rows[i]["GroupID"].ToString();
            cmd2.Parameters.Add("@Acc_Name", SqlDbType.VarChar).Value = dtTemp.Rows[i]["NameID"].ToString();
            cmd2.Parameters.Add("@Particulars", SqlDbType.VarChar).Value = dtTemp.Rows[i]["Particular"].ToString();
            cmd2.Parameters.Add("@ChequeNo", SqlDbType.VarChar).Value = dtTemp.Rows[i]["ChequeNo"].ToString();
            cmd2.Parameters.Add("@Debit", SqlDbType.VarChar).Value = dtTemp.Rows[i]["Debit"].ToString();
            cmd2.Parameters.Add("@Credit", SqlDbType.VarChar).Value = dtTemp.Rows[i]["Credit"].ToString();
            cmd2.ExecuteNonQuery();
        }
        con.Close();
        GridfinalBind();//binding gridview


//below is nothing but a way of inserting datatable record into server table but i m already using above method so u dont have to worry about below code....its all commented!!
        //foreach (DataRow row in TempDTable.Rows)
        //{
        //    SqlDataAdapter sqlInsert = new SqlDataAdapter();
        //    sqlInsert.InsertCommand = con.CreateCommand();
        //    sqlInsert.InsertCommand.CommandText = "INSERT INTO ACE_SALE_CONTRACT_UPDATE (ITEM_ID, ITEM_NO) SELECT '" +
        //    (row["ITEM_ID"]) + "' AS ITEM_ID, '" +
        //    (row["ITEM_NO"]) + "' AS ITEM_NO";
        //    sqlInsert.InsertCommand.ExecuteNonQuery();
        //}


and here is the final running sample...kindly attach your own webconfig and run it!

Thursday, 3 May 2012

Print Out Only Grid View Or Any Fix Content Area In Asp.net Page

Print out only Grid view,Datalist,Detail view or Any fixed content area like <div> in asp.net page

Some time you have so many things in your single page lets say a GRIDVIEW,a DATALIST,a DIV ,or any other(server control which has a ID)from which you want to print the individual content and not the whole page. From this i mean how to print a fix content area from whole page in asp.net,lets see how easly it could be done.



Just take a look at below webpage .
Now as a developer how could you  print out only red marked areas with a print button click.








In case you need help lets proceed with this tutorial...

Lets take brief look at simple screen below

 






Its simply include
a div with id name printable and runat="server"
then my text under that div
and some text outside that div


 Here div is server control  as i made it by giving it a id name and runat="server" as below

<div runat="server" id="printable">
          
 </div>




Similarly it could be any control either Datalist,Gridview or any Control
Now in final there is image button...(you don't have to fire its click event!)


Just write this code in Page_Load 
and at the time you click it ,Our page will automatically recognize it and give you the print out option!


 protected void Page_Load(object sender, EventArgs e)
    {
            string printScript =@"function PrintGridView()
         {
            var gridInsideDiv = document.getElementById('printable');
            var printWindow = window.open('gridview.htm','PrintWindow','letf=0,top=0,width=850,height=300,toolbar=1,scrollbars=1,status=1');
            printWindow.document.write(gridInsideDiv.innerHTML);
            printWindow.document.close();
            printWindow.focus();
            printWindow.print();
            printWindow.close();}";
            this.ClientScript.RegisterStartupScript(Page.GetType(), "PrintGridView", printScript.ToString(), true);
            btnPrint.Attributes.Add("onclick", "PrintGridView();"); 
   }


and finaly the sample code is here...



Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...