Friday, May 17, 2013

DataGridView Paging in c# WinForm


 I have a window  project in which I had to show  specific number of records and that that specific number of records  will display in each next and previous button . I have used list<> for the datasource  For Datagridview.



        int skip = 0, take = 0, nextCount=0;
  private void StoreRoomBM_Load(object sender, EventArgs e)
        {
            FillAllLoadTime();
           
        }


      void FillAllLoadTime()
        {
          
            var a = from p in lstDetails.Take(5)
                     select p;                 
            if (a.ToList().Count > 0)
            {
                int showtotal = a.ToList().Count;
                DataDetailsData.DataSource = a.ToList();
                LblREcordCount.Text = 1 + " - " + showtotal + "  of " +   lstDetails.Count;
                btnPrevious.Enabled = false;
                LblRecord.Visible = false;
            }
            else
            {

                DataDetailsData.DataSource = null;
                LblRecord.Visible = true;
            }

         

        }

private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
               
               nextCount ++;
                for (int i = 1; i < LstDetails.ToList().Count/5 + 1 ; i++)
                {
                    if (nextCount == i)
                    {
                        
                        skip = skip + 5;
                        take =  5;
                        var a = from p in LstDetails.Skip(skip).Take(take)
                                select p;
                        if (a.ToList().Count > 0)
                        {
                            btnPrevious.Enabled = true;
                            DataDetailsData.DataSource = a.ToList();
                            LblREcordCount.Text = (skip + 1) + " - " + (skip + take) + "  of " + LstDetails.Count;
                            LblRecord.Visible = false;
                        }
                        else
                        {

                            DataDetailsData.DataSource = null;
                            LblRecord.Visible = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
        }
private void btnPrevious_Click(object sender, EventArgs e)
        {
            try
            {

                nextCount--;
                for (int i = 0; i < LstDetails.ToList().Count / 5 ; i++)
                {
                    if (nextCount == i)
                    {

                        skip = skip - 5;
                        take = 5;
                        var a = from p in LstDetails.Skip(skip).Take(take)
                                select p;
                        if (a.ToList().Count > 0)
                        {
                            
                            DataDetailsData.DataSource = a.ToList();
                            LblREcordCount.Text = (skip + 1) + " - " + (skip + take) + "  of " + LstDetails.Count;
                            LblRecord.Visible = false;
                        }
                        else
                        {

                            DataDetailsData.DataSource = null;
                            LblRecord.Visible = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
        }

Thursday, March 7, 2013

How to create Dynamic Tab in ASP.Net



  I have a project where a page has a tab control with two static tabs. But more tabs and controls can be added with those fixed tab dynamically.These tabs and controls are defined in masters properly. It was really a interesting and challenging work for me. Also DataBase operations are there. For this project I needed AJAX tab container (N.B: how to use AJAX in Asp.Net? Please search and follow the procedure). Here I have used LINQ . I am retrieving data from Master Table and creating Tab and Controls Dynamically

N.B: It is not bound that we will use LINQ . You can use other way like DataReader or  DataAdaptor




Step-1

  Static Design View :

<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:TabContainer ID="TabContainer1" runat="server">
         <asp:TabPanel ID="TabGeneral" runat="server">
           <HeaderTemplate>General_1</HeaderTemplate>
           <ContentTemplate>
               //Add Controls As per your requirements
           </ContentTemplate>
         </asp:TabPanel>


        <asp:TabPanel ID="TabGeneral1" runat="server">
           <HeaderTemplate>General_2</HeaderTemplate>
           <ContentTemplate>
               //Add Controls As per your requirements
           </ContentTemplate>
         </asp:TabPanel>
        </asp:TabContainer>
    </div>
    </form>
</body>



Step-2


  Dynamic Code :


 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;



namespace Website

{
    public partial class Inventory_DynamicTab : System.Web.UI.Page
    {


      //Declare Variables and Objects
        TabPanel pnl;
        Table tbl;
        TableRow tbr;
        TableCell tcl;
        Button objButton;
        Button objButtonReset;
        Button objButtonSelect;   
        DropDownList objDDL;
        Label objLavel;      
        TextBox objTextBox;      
        CalendarExtender objCalender;


       protected void Page_PreInit(object sender, EventArgs e)
          {

            try
            {

                fillTabname();
            }
            catch (Exception ex)
            {
            }
         }

        void fillTabname()
        {

            var lnqParameter = (from Item in GetTabSequence() // own query to get data from DB
                                orderby Item.Sequence
                                select Item.TabName).Distinct();
            if (lnqParameter.ToArray().Length > 0)
            {
                for (int i = 0; i < lnqParameter.ToList().Count; i++)
                {

                     pnl = new TabPanel();
                    pnl.HeaderText = lnqParameter.ToList()[i].ToString();
                    pnl.ID = (i).ToString();
                    TabContainer1.Tabs.Add(pnl);


                    tbl = new Table();
                    tbl.BorderColor = System.Drawing.Color.Black;
                    tbl.ID = i.ToString();

                    TabContainer1.Tabs[i + 1].Controls.Add(tbl);

                    //// table
                    var lnqParameterCount = from Item in GetSequence()
// own query to get data from DB
                                            orderby Item.Sequence
                                            where Item.TabName == pnl.HeaderText 

                                            select new
                                            {
                                                fieldId = Item.Field_ID,
                                                fieldNm = Item.FieldName,
                                                fieldNm_Id = Item.Field_Name_ID,
                                                fieldType = Item.Control_type,
                                                multipleRow = Item.Multiple_Rows,
                                                sequence = Item.Sequence,
                                                maxlength = Item.Max_Length
                                            };
                    if (lnqParameterCount.ToList().Count > 0)
                    {

                        for (int j = 0; j < lnqParameterCount.ToList().Count; j++)
                        {                          

                            objLavel = new Label();
                            objTextBox = new TextBox();
                            objButton = new Button();


                            if (j <= 6)
                            {
                                if (lnqParameterCount.ToList()[j].fieldType == "TextBox" )
                                {
                                    //________________________
                                    objLavel.Text = lnqParameterCount.ToList()[j].fieldNm.ToString() + " :";
                                    objLavel.Width = 90;
                                    objLavel.Height = 15;
                                    objLavel.Font.Size = FontUnit.Smaller;
                                    //_____________________________

                                    objTextBox.ID = lnqParameterCount.ToList()[j].fieldId.ToString() + "_" + "Txt" + lnqParameterCount.ToList()[j].fieldNm_Id.ToString() + "_" + pnl.ID;
                                    objTextBox.Width = 120;
                                    objTextBox.Height = 15;
                                    objTextBox.MaxLength = int.Parse(lnqParameterCount.ToList()[j].maxlength.ToString());



                                    tcl = new TableCell();
                                    tcl.Controls.Add(objLavel);
                                    tcl.Controls.Add(objTextBox);


                                    tbr = new TableRow();
                                    tbr.Cells.Add(tcl);
                                    tbr.BackColor = System.Drawing.Color.Cyan;
                                    tbr.Width = 100;
                                    tbr.BorderWidth = 5;
                                    tbl.Rows.Add(tbr);


                                }
                                else if (lnqParameterCount.ToList()[j].fieldType == "Dropdown" )
                                {

                                    objLavel.Text = lnqParameterCount.ToList()[j].fieldNm.ToString() + " :";
                                    objLavel.Width = 90;
                                    objLavel.Height = 15;
                                    objLavel.Font.Size = FontUnit.Smaller;
                                    //________________________                              
                                    //objComboBox = new ComboBox();
                                    objDDL = new DropDownList();
                                    objDDL.ID = lnqParameterCount.ToList()[j].fieldId.ToString() + "_" + "Cmb" + lnqParameterCount.ToList()[j].fieldNm_Id.ToString() + "_" + pnl.ID;
                                    objDDL.Width = 120;
                                    objDDL.Height = 20;
                                    objDDL.SelectedIndexChanged+=new EventHandler(objDDL_SelectedIndexChanged);


                                    tcl = new TableCell();
                                    tcl.Controls.Add(objLavel);
                                    tcl.Controls.Add(objDDL);


                                    tbr = new TableRow();
                                    tbr.Cells.Add(tcl);
                                    tbr.BackColor = System.Drawing.Color.Cyan;
                                    tbr.Width = 100;
                                    tbr.BorderWidth = 5;
                                    tbl.Rows.Add(tbr);


                                    var getCmbData = from data in GetSequenceDetails()
// own query to get data from DB
                                                     where data.ParameterID == int.Parse(lnqParameterCount.ToList()[j].fieldId.ToString()) && data.Reference_Parameter_ID == 0
                                                     select data;

                                    if (getCmbData.ToList().Count > 0)
                                    {

                                        List<TAGGING_Property> colors = new List<TAGGING_Property>();
                                        TAGGING_Property objcol = new TAGGING_Property();
                                        objcol.Data_Value = "Field_ID";
                                        objcol.Data_Display = "Select";
                                        colors.Add(objcol);
                                        foreach (TAGGING_Property country in getCmbData.ToList())
                                        {
                                            colors.Add(country);
                                        }
                                        string idd = objDDL.ID.ToString();
                                        objDDL.DataSource = colors;
                                        objDDL.DataTextField = "Data_Display";
                                        objDDL.DataValueField = "Field_ID";
                                        objDDL.DataBind();
                                        objDDL.SelectedIndex = 0;
                                    }
                                }
                                else if (lnqParameterCount.ToList()[j].fieldType == "Calender" )
                                {
                                    objCalender = new CalendarExtender();
                                    //________________________
                                    objLavel.Text = lnqParameterCount.ToList()[j].fieldNm.ToString() + " :";
                                    objLavel.Width = 90;
                                    objLavel.Height = 15;
                                    objLavel.Font.Size = FontUnit.Smaller;
                                    //_____________________________

                                    objTextBox.ID = lnqParameterCount.ToList()[j].fieldId.ToString() + "_" + "Txt" + lnqParameterCount.ToList()[j].fieldNm_Id.ToString() + "_" + pnl.ID;
                                    objTextBox.Width = 120;
                                    objTextBox.Height = 15;
                                    objTextBox.MaxLength = int.Parse(lnqParameterCount.ToList()[j].maxlength.ToString());
                                    objCalender.TargetControlID = objTextBox.ID;
                                    objCalender.Format = "dd/MM/yyyy";


                                    tcl = new TableCell();
                                    tcl.Controls.Add(objLavel);
                                    tcl.Controls.Add(objTextBox);
                                    tcl.Controls.Add(objCalender);

                                    tbr = new TableRow();
                                    tbr.Cells.Add(tcl);
                                    tbr.BackColor = System.Drawing.Color.Cyan;
                                    tbr.Width = 100;
                                    tbr.BorderWidth = 5;
                                    tbl.Rows.Add(tbr);

                                }
                            }                      

                        }                    
                    }



                    objButton = new Button();
                    objButton.Text = "Save";
                    objButton.ID = pnl.ID + objButton.Text;
                    objButton.Width = 80;
                    objButton.Height = 25;
                    objButton.Click += new EventHandler(objButton_Click);
                    pnl.Controls.Add(objButton);

                    objButtonReset = new Button();
                    objButtonReset.Text = "Reset";
                    objButtonReset.ID = pnl.ID + objButtonReset.Text;
                    objButtonReset.Width = 80;
                    objButtonReset.Height = 25;
                    objButtonReset.Click+=new EventHandler(objButtonReset_Click);
                    pnl.Controls.Add(objButtonReset);

                }
            }
        }



       




      





Wednesday, March 6, 2013

How to loop through all the controls in Active Tab of ASP.NET Ajax TabContainer



 In this code I have cleared all  Textbox of my Active Tab Panel  in the AJAX Tabcontainer control.

Code:-


 AjaxControlToolkit.TabContainer container = (AjaxControlToolkit.TabContainer)TabContainer1;        
                foreach (object obj in container.ActiveTab.Controls)
                {
                    if (obj is Control)
                    {
                        Control c = (Control)obj;
                        if (c is System.Web.UI.WebControls.Table)
                        {
                            Table tbl = c as Table;
                            foreach (object rw in tbl.Rows)
                            {
                                TableRow tbr = rw as TableRow;
                                foreach (object cl in tbr.Cells)
                                {
                                    TableCell tcl = cl as TableCell;
                                    foreach (object ctl in tcl.Controls)
                                    {

                                        if (ctl is System.Web.UI.WebControls.TextBox)
                                        {
                                            TextBox txt = ctl as TextBox;                                          
                                            txt.Text = "";
                                        }

                                    }

                                }
                            }

                        }
                    }

                }
            }
        }
 

Tuesday, January 29, 2013

Enter only Numeric Data in TextBox


private void TxtQuantity_KeyPress(object sender, KeyPressEventArgs e)
        {
if (char.IsDigit(e.KeyChar) == false && char.IsLetter(e.KeyChar) == false && char.IsControl(e.KeyChar) == false && e.KeyChar != '.' && e.KeyChar != ' ')
            {
                e.Handled = true;
                MessageBox.Show("You can enter only number.", "KenCloud", MessageBoxButtons.OK, MessageBoxIcon.Information);
                TxtQuantity.Clear();
                TxtQuantity.Focus();
                return;
            }
}

Enter only Numeric data in Datagridview in c#


private void DgviewStock_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            if ((e.ColumnIndex == 10))
            {
                string value = DgviewStock.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                foreach (char c in value)
                {
                    if (!char.IsDigit(c))
                    {
                        MessageBox.Show("Please enter numeric value.");
                        DgviewStock.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = string.Empty;
                        return;
                    }
                }
            }
        }

Common Report Viewer for All CrystalReport


namespace Barcodes.Reports
{
    public partial class frmReportViewer : Form
    {
        public string paramvalue;
        public int reportFlag,paramvalueId;

        public frmReportViewer( )
        {
            InitializeComponent();
       
        }

        private void frmReportViewer_Load(object sender, EventArgs e)
        {

            TAGGING_CommonVariable objVar = new TAGGING_CommonVariable();
            objVar.servername = @"SATYA";
            objVar.DatabaseName = "Admission";
            objVar.username = "sa";
            objVar.password = "sql";        

            if (reportFlag == 0)//IssueToQualityControl
            {
                crReport objCr = new crReport();               
                CrystalDecisions.Shared.TableLogOnInfo tliCurrent = default(CrystalDecisions.Shared.TableLogOnInfo);
                try
                {
                    foreach (CrystalDecisions.CrystalReports.Engine.Table tbCurrent in objCr.Database.Tables)
                    {
                        tliCurrent = tbCurrent.LogOnInfo;
                        var _with1 = tliCurrent.ConnectionInfo;
                        _with1.ServerName = objVar.servername;
                        _with1.DatabaseName = objVar.DatabaseName;                      
                        _with1.UserID = objVar.username;
                        _with1.Password = objVar.password;
                        _with1.IntegratedSecurity = false;
                        tbCurrent.ApplyLogOnInfo(tliCurrent);
                    }

                    this.crystalReportViewerAll.ReportSource = objCr;
                    objCr.SetParameterValue(0, paramvalue);
                    objCr.SetParameterValue(1, paramvalueId);
                    crystalReportViewerAll.Show();

                }                 

                catch
                {
                }

            }
             }
}

Case sensitive for Sql select in Sql Server



Create PROCEDURE  SelectDetails
(
@Name varchar(30)
)
AS
BEGIN

    if (select name from  Student where Name=@Name ) is NULL
begin

SELECT       Name, Age ,Address    FROM   Student where Name=@Name
COLLATE  SQL_Latin1_General_CP1_CS_AS

        end