Wednesday, 25 June 2014

Adding items to datatable dynamically

No comments
Adding rows in datatable
     
        DataTable dt = new DataTable();
        if (Session["data"] != null)
                {
                    dt = (DataTable)Session["data"];
                }
                else
                {
                    DataColumn proid = new DataColumn("p_id");
                    DataColumn Product_id = new DataColumn("Product");                 

                    dt.Columns.Add(proid);
                    dt.Columns.Add(Product);                
                }
                DataRow dr = dt.NewRow();
                dr["p_id"] = productid.Text;
                dr["Product"] = pid.Text;                            
                dt.Rows.Add(dr);
                Session["data"] = dt;

Updating data in datatable

 protected void Edit_Click(object sender, EventArgs e)
     {      
        Button im = (Button)sender;
        GridViewRow gr = (GridViewRow)im.NamingContainer;
        DataTable dt = (DataTable)Session["myDt"];
        DataRow dr = dt.Rows[gr.RowIndex];
        Session["dr"] = dr;
        Label lblpro = (Label)GridView1.Rows[gr.RowIndex].FindControl("lblpronm");
        texttBox1.text=lblpro.text;
        }   

  protected void Update_Click(object sender, EventArgs e)
     {    
            DataTable dt = (DataTable)Session["myDt"];
            DataRow dr = (DataRow)Session["dr"];
            dr["Proname"] = lblpro.text;         
            GridView1.DataSource = dt;
            GridView1.DataBind();
      }

Deleting rows from datatable

 protected void Delete_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton img = (ImageButton)sender;
        GridViewRow gr = (GridViewRow)img.NamingContainer;
        DataTable dt = (DataTable)Session["data"];
        DataRow dr = dt.Rows[gr.RowIndex];
        dr.Delete();
        GridView1.DataSource = dt;
        GridView1.DataBind();
      
    }

Inserting data from datatable to database

protected void btn_checkout_Click(object sender, EventArgs e)
    {
 DataTable dt = (DataTable)Session["data"];
for (int j = 0; j < dt.Rows.Count; j++)
        {
            dl.p_id = Convert.ToInt16(dt.Rows[j]["p_id"]);
            dl.products = Convert.ToInt16(dt.Rows[j]["Product"]);
            bl.insert_order(dl);                                             
        }
read more

Monday, 23 June 2014

AccordionPane in ajax

No comments
.css

 .accheader
    {
        background-color:#2E4D7B;
        border:1px solid #2F4F4F;
        color:White;
        cursor:pointer;
        font-family: Arial,Sans-Serif;
        font-size:12px;
        font-weight:bold;
        margin-top:5px;
        padding:5px;
        width:730px;
       
       
   
    }
    .accheaderselected
    {
        background-color:#5078B3;
        border:1px solid #2F4F4F;
        color:White;
        cursor:pointer;
        font-family:Arial,Sans-Serif;
        font-size:12px;
        font-weight:bold;
        margin-top:5px;
        padding:5px;
        width:730px;
    }
    .acccontent
    {
        background-color:#D3DEEF;
        border-color:-moz-use-text-color #2F4F4F #2F4F4F;
        border-right: 1px dashed #2F4F4F;
        border-style: none dashed dashed;
        border-width: medium 1px 1px;
        padding: 10px 5px 5px;
        width:715px;
       
    }
 

.aspx


div class="accordation">
    <asp:Accordion ID="Accordion1" runat="server"
        FramesPerSecond="40" RequireOpenedPane="false" SuppressHeaderPostbacks="True"
        TransitionDuration="255" Enabled="true" HeaderCssClass="accheader"
        HeaderSelectedCssClass="accheaderselected" ContentCssClass="acccontent" FadeTransitions="true" AutoSize="Limit" SelectedIndex="0">
        <Panes>
            <asp:AccordionPane ID="AccordionPane1" runat="server">
            <Header><a href="" class="href">Articles</a></Header>
            <Content >
            <ul>
            <li>Animation/ Multimedia</li>
            <li>Engineering/ Technology</li>
           </ul>
             </Content>           
            </asp:AccordionPane>
        </Panes>
      </asp:Accordion>
</div>
  
read more

Saturday, 21 June 2014

Sorting in gridview

No comments
 public SortDirection dir
    {
        get
        {
            if (ViewState["dirState"] == null)
            {
                ViewState["dirState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["dirState"];
        }
        set
        {
            ViewState["dirState"] = value;
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        all_resol();
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand("select* from Details whereActive='Active' and Status=1 ", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        dt = ds.Tables[0];
        {
            string SortDir = string.Empty;
            if (dir == SortDirection.Ascending)
            {
                dir = SortDirection.Descending;
                SortDir = "Desc";
            }
            else
            {
                dir = SortDirection.Ascending;
                SortDir = "Asc";
            }
            DataView sortedView = new DataView(dt);
            sortedView.Sort = e.SortExpression + " " + SortDir;
            GridView1.DataSource = sortedView;
            GridView1.DataBind();
        }
    }


 <asp:TemplateField HeaderText="IS" SortExpression="IS">
</asp:TemplateField>
read more

Friday, 20 June 2014

show online users

No comments
Global.asax

Application_start
{
    Application["unm"] =0;
}
Application_end
{
   Application["unm"] =0;
}
Session_start
{
   Application.lock();
   Application["unm"] =Convert.toInt16(Application["unm"]+1);
   Application.Unlock();
}Session_end
{
   Application.lock();
   Application["unm"] =Convert.toInt16(Application["unm"]-1);
   Application.Unlock();
}

.cs file

label1.text="online users"+Application["unm"];
read more

Friday, 30 May 2014

Redirect to another page after few second

No comments
in cs

protected void Page_Load(object sender, EventArgs e)
    {
        Response.AddHeader("REFRESH", "2;URL=login.aspx");
    }

or

 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ScriptKey", "alert('ok');window.location='Transfer.aspx'; ", true);

mouse hover and out on linkbutton

 LinkButton1.Attributes.Add("onmouseover", "this.style.color=\"green\"");
 LinkButton1.Attributes.Add("onmouseout", "this.style.color=\"black\"");
read more

Add data from excel file to database

No comments
To show excel data in gridview

protected void btnShowExcelData_Click(object sender, EventArgs e)
    {
        string currpath = Server.MapPath("~/upload/payment.xlsx");
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + currpath + ";Extended Properties=Excel 12.0";

        OleDbConnection oledbConn = new OleDbConnection(connString);
        try
        {

            oledbConn.Open();


            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);

          
            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
            // Pass the Select command to the adapter.
            objAdapter1.SelectCommand = cmd;
            // Create new DataSet to hold information from the worksheet.
            DataSet objDataset1 = new DataSet();
            // Fill the DataSet with the information from the worksheet.
            objAdapter1.Fill(objDataset1, "ExcelData");
            GridView1.DataSource = objDataset1;
            GridView1.DataBind();

        }
        catch (Exception ex)
        {
            lblInserted.Text = "" + ex;
        }
        finally
        {

            oledbConn.Close();
        }
    }


To insert data

protected void btnInsert_Click(object sender, EventArgs e)
    {
        GridView2.Visible = true;
        string currpath = Server.MapPath("~/upload/payment.xlsx");
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + currpath + ";Extended Properties=Excel 12.0";

        OleDbConnection oledbConn = new OleDbConnection(connString);


        oledbConn.Open();


        OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
        OleDbDataReader dr = cmd.ExecuteReader();


        while (dr.Read())
        {
          
            DataSet1TableAdapters.outstandingTableAdapter da1 = new DataSet1TableAdapters.outstandingTableAdapter();
            DataSet1.outstandingDataTable dt1 = new DataSet1.outstandingDataTable();
            DataSet1.outstandingRow dr0;
            dr0 = dt1.NewoutstandingRow();
            dr0.clientCode = dr[1].ToString();
            dr0.outstanding = dr[2].ToString().Trim();

            dr0.billValue = Convert.ToString(dr[3]);
            dr0.billcValue = dr[4].ToString();
            dr0.name = Convert.ToString(dr[5]);
            dr0.uname = Convert.ToString(dr[6]);
            dr0.EntryDateTime = Convert.ToDateTime(dr[7]);

            dt1.Rows.Add(dr0);
            lblInserted.Text = "Insetred";
            try
            {
               
                da1.Update(dt1);
            }
            catch (SqlException ex)
            {
                lblmsg.Text = "" + ex;
            }
        }
 
 
read more

Thursday, 29 May 2014

Adding rows on button click

No comments
.ASPX

<form id="form1" runat="server">
    <div>
       <asp:Panel ID="p1" runat="server">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server">
            
       
        </asp:PlaceHolder>
        </asp:Panel>
        <br />
        <br />
        <asp:Button ID="btnadd" runat="server" onclick="btnadd_Click" Text="Add" />
       
    </div>
    </form>

.CS

static int number_rows = 1;
    protected void Page_Init(object sender, EventArgs e)
    {
        ViewState["RowsCount"] = number_rows    
    }
 protected void Page_Load(object sender, EventArgs e)
    {
 if (!IsPostBack)
        {
            generate_table();
        }
        else
        {
              string CtrlID = string.Empty;
         if (Request.Form["__EVENTTARGET"] != null &&
                     Request.Form["__EVENTTARGET"] != string.Empty)
            {
                generate_table();             
            }
            else
            {
                //to check which button or image button click caused the postback
                foreach (string controlID in Request.Form)
                {
                    Control objControl = Page.FindControl(controlID);
                    if (objControl is Button)
                    {
                        CtrlID = objControl.ID;
                        if (CtrlID == "btnadd")
                        {

                            //now the call will go to Button1_Click function
                        }                      
                    }
                }
            }
        }
    }
 protected void generate_table()
    {
        Panel p1 = new Panel();
        Table table = new Table();
        TableRow row,row1;
        TableCell cell,cell1;
        TextBox tb,tb1;
        Label lbl,lbl1;
        table.ID = "Table1";
        p1.ID = "p1";
        int s_rows = Convert.ToInt32(ViewState["RowsCount"].ToString());

        for (int k = 1; k <= s_rows; k++)
        {
            row = new TableRow();
            cell = new TableCell();
            row1= new TableRow();
            cell1 = new TableCell();

            tb = new TextBox();
            tb.ID = "tb_" + k;          
            tb.AutoPostBack = true;

            tb1 = new TextBox();
            tb1.ID = "tb1_" + k;
            tb1.AutoPostBack = true;

            lbl = new Label();
            lbl.ID = "lbl" + k;
            lbl.Text = "name";

            lbl1 = new Label();
            lbl1.ID = "lbl1" + k;
            lbl1.Text = "Address";

            cell.Controls.Add(lbl);
            cell.Controls.Add(tb);

            cell1.Controls.Add(lbl1);
            cell1.Controls.Add(tb1);
            tb.Style.Add(HtmlTextWriterStyle.MarginLeft, "120px");
            tb1.Style.Add(HtmlTextWriterStyle.MarginLeft, "104px");
            row.Cells.Add(cell);
            table.Rows.Add(row);

            row1.Cells.Add(cell1);
            table.Rows.Add(row1);           
        }

        PlaceHolder1.Controls.Add(table);       
    }

    protected void btnadd_Click(object sender, EventArgs e)
    {
        int new_rows_count = Convert.ToInt32(ViewState["RowsCount"]) + 1; //add one rows at a time
        ViewState["RowsCount"] = new_rows_count;
        generate_table();
        setdata();
    }
protected void setdata()
    {
        Table table = (Table)Page.FindControl("Table1");
        if (table != null)
        {

            foreach (TableRow tr in table.Rows)
            {
                foreach (TableCell tc in tr.Cells)
                {
                    foreach (Control ct in tc.Controls)
                    {
                        if (ct is TextBox)
                        {
                            ((TextBox)ct).Text = Request.Form[ct.ID];
                        }
                      
                    }
                }
            }
        }
    }

}
read more