Tuesday, 18 December 2012

session c#.net session timeout


protected void btnLearnerSubmit_Click(object sender, ImageClickEventArgs e)

    {
       
        b.flag = 1;
        b.Learnerloginid = txtLearnerName.Text;
        b.password = txtLearnerPass.Text;

     
        DataTable dt = b.login();
        string learnerid =dt.Rows[0][0].ToString();

        Session["learnerloginid"] = learnerid;

        string URL = "LearnerDetails.aspx?id=" + learnerid;
        Response.Redirect(URL);
    }

LearnerDetails.aspx:

string id;

  if (IsPostBack != null)
        {
            id = Session["learnerloginid"].ToString();
            Label1.Text = id.ToString();


            HtmlMeta meta = new HtmlMeta();
            meta.HttpEquiv = "Refresh";
            meta.Content = Convert.ToString(Session.Timeout * 60) + ";url=ViewSubject.aspx";
            this.Page.Header.Controls.Add(meta);
           

        }



  <sessionState mode="InProc" cookieless="true" timeout="1">
     
    </sessionState>





Friday, 9 November 2012

to edit,update in gridview

 <div>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False"
            GridLines="None" OnRowDataBound="GridView1_RowDataBound" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowEditing="GridView1_RowEditing">
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:TemplateField HeaderText="Stateid">
                    <ItemTemplate>
                        <asp:Label ID="lblstateid" runat="server" Text='<%#Bind("Stateid") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                    <ItemTemplate>
                        <asp:TextBox ID="txtState" runat="server" Text='<%#Bind("State") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Location">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlcity" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="edit" />
                        <asp:Button ID="btndelete" runat="server" Text="delete" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Button ID="btnUpdate" runat="server" Text="Update" OnCommand="Update" />
                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField></asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>

public void bindgridview()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select Stateid,State from tbl_State", con);
        SqlDataAdapter sdp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sdp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        con.Close();
    }
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindgridview();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        bindgridview();   
    }
    protected void Update(object sender, CommandEventArgs e)
    {
        con.Open();
        Button edit = (Button)sender;
        TextBox txtpackage = (TextBox)edit.FindControl("txtState");
        Label id = (Label)edit.FindControl("lblstateid");
        string query = "Update tbl_State set State=@State where Stateid=@Stateid";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@State", txtpackage.Text);
        cmd.Parameters.AddWithValue("@Stateid", id.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        bindgridview();  

    }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)// method2
    {
        con.Open();
        TextBox txtpackage = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtState");
        Label id = (Label)GridView1.Rows[e.RowIndex].FindControl("lblstateid");
        string query = "Update tbl_State set State=@State where Stateid=@Stateid";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@State", txtpackage.Text);
        cmd.Parameters.AddWithValue("@Stateid", id.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        bindgridview();
     
     

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) method 3
    {
        con.Open();
        GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
        TextBox txtpackage = (TextBox)GridView1.Rows[row.RowIndex].FindControl("txtState");
        Label id = (Label)GridView1.Rows[row.RowIndex].FindControl("lblstateid");
        string query = "Update tbl_State set State=@State where Stateid=@Stateid";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@State", txtpackage.Text);
        cmd.Parameters.AddWithValue("@Stateid", id.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        bindgridview();


}

Find Control or Datakeys on RowCommand Event of GridView : 

To Find Control onRowCommand Event in GridView there is no Simple Way But we can use simple technique to find control from GridView.
This Simple Technique is given as follows:
1. Create a object of GridViewRow from refrence of command source row.
For Eg: 
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer; 

But keep in mind that which type of object control is you are using to call event in above code is LinkButton. But if You Calling Rowcommand event from Simple Button then You need to Write this:
GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;

2. After Creating Row Object You can simply find any control from Gridview using rowindex.
For Eg: 
Label LabelEmail = (Label)GridView1.Rows[row.RowIndex].FindControl("LabelEmail");

3. Finally You Can Access that Properties of that Control.
For Eg: 
LabelResult.Text = LabelEmail.Text; 

4. Same as Above You can also find Datakeys of gridview using GridViewRow rowindex.
For Eg: 
int code = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0].ToString()); 

Thursday, 25 October 2012

Dynamic Datagridview button click


gridview bind value:
 public void gridview()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select contactid,firstname from ContactInfo", con);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            dataGridView1.DataSource = dt;
          con.Close();
        }

 private void Form1_Load(object sender, EventArgs e)
        {
            gridview();
            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(btn);
            btn.HeaderText = "Edit";
            btn.Text = "Edit";
            btn.Name = "btn";
            btn.UseColumnTextForButtonValue = true;
            dataGridView1.AutoResizeColumns(
            DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
                  
        }


  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            if (dataGridView1.Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                textBox1.Text = dataGridView1.CurrentRow.Cells["firstname"].Value.ToString();
                txtSearch.Text = dataGridView1.CurrentRow.Cells["contactid"].Value.ToString();
            }   
        }




Tuesday, 2 October 2012

combobox bindinglist list in c#:


private void combo_item()
        {
            var card_name = new BindingList<KeyValuePair<string, string>>();
            card_name.Add(new KeyValuePair<string, string>("VISA", "VISA"));
            card_name.Add(new KeyValuePair<string, string>("Master Card","MASTER CARD"));
            card_name.Add(new KeyValuePair<string, string>("Maestro Card", "MAESTRO CARD"));
            cmbcardname.DataSource = card_name;
            cmbcardname.ValueMember = "Key";
            cmbcardname.DisplayMember = "Value";
            cmbcardname.SelectedIndex = 1;
            var card_type = new BindingList<KeyValuePair<string, string>>();
            card_type.Add(new KeyValuePair<string, string>("Credit Card", "CREDIT CARD"));
            card_type.Add(new KeyValuePair<string, string>("Debit Card", "DEBIT CARD"));
            cmbcardtype.DataSource = card_type;
            cmbcardtype.ValueMember = "Key";
            cmbcardtype.DisplayMember = "Value";
            cmbcardtype.SelectedIndex = 1;
        }

Monday, 1 October 2012

Linq search in textbox bind an listbox c#

Linq search in textbox bind an listbox c#



con.Open();
            SqlDataAdapter adp = new SqlDataAdapter("select awardname from Award", con);
            //DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            adp.Fill(ds);
            var r = from dr in ds.Tables[0].AsEnumerable()
                    where dr.Field<string>("awardname") == txtSearch.Text
                    select dr.Field<string>("awardname");

            if (r.Count() > 0)
            {
                listBox1.DisplayMember = "awardname";
                listBox1.DataSource = r.ToList();

            }
            //foreach (string s in r)
            //{
            //    //label1.Text = s;
            //    listBox1.DisplayMember = "awardname";

            //}
     
           con.Close();
        }
        public void bind()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select awardname from Award", con);
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            listBox1.DataSource = dt;
            listBox1.DisplayMember = "awardname";
            con.Close();

        }

Friday, 24 August 2012

show and hide panel


 <script src="jquery-1.7.2.min.js" type="text/javascript"></script>
  <script type="text/javascript">
       $(document).ready(function() {

           $("div.cpBody").hide();
           $("#btnHide").hide();
           var panelArray = $('div.cpBody').length;
           var temp = -1;

           $("#btnShow").click(function displayPanel() {
               if (temp < panelArray) {
                   $('div.cpBody').eq(++temp).fadeIn(100, function() {
                       displayPanel();
                       if (temp == panelArray) {
                           $("#btnHide").show();
                           $("#btnShow").hide();
                       }
                   });
               }
           });


           $("#btnHide").click(function hidePanel() {
               if (temp >= 0) {
                   $('div.cpBody').eq(--temp).fadeOut(100, function() {
                       hidePanel();
                   });
                   if (temp < 0) {
                       $("#btnHide").hide();
                       $("#btnShow").show();
                       temp = -1;
                   }
               }
           });

       });
   </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
        <div class="cpHeader">
            <asp:ImageButton ID="btnShow" ImageUrl="~/Images/Show.gif" runat="server" OnClientClick="return false;" />
            <asp:ImageButton ID="btnHide" ImageUrl="~/Images/Hide.gif" runat="server" OnClientClick="return false;" />          
        </div>  

        <asp:Panel ID="Panel1" runat="server" class='cpBody'>
            <asp:Label ID="Label1" runat="server" Text="Label">Panel 1 Content</asp:Label>
        </asp:Panel>
        <asp:Panel ID="Panel2" runat="server" class='cpBody'>
            <asp:Label ID="Label2" runat="server" Text="Label">Panel 2 Content</asp:Label>
        </asp:Panel>
        <asp:Panel ID="Panel3" runat="server" class='cpBody'>
            <asp:Label ID="Label3" runat="server" Text="Label">Panel 3 Content</asp:Label>
        </asp:Panel>

    </div>
   
    </div>
    </form>
</body>
</html>

Wednesday, 1 August 2012

DropDown state,City 3 tier architecture

BAL:
declare in string,int variable get and set

public int tablelogin()
    {
       SqlParameter[] p=new SqlParameter[5];
        p[0]=new SqlParameter("UserName",username);
        p[1]=new SqlParameter("State",state);
        p[2]=new SqlParameter("City",city);
        p[3]=new SqlParameter("Ip",ip);
        p[4]=new SqlParameter("LoginTime",logintime);
        int result=DAL.ExecuteNonQuery_login("Insert_Login",p);
        return result;
    }
    public DataSet load()
    {
        String query = ("select * from dbo.tbl_login");
        DataSet ds = DAL.load(query);
        return ds;
    }
    public DataTable getState()
    {
        String query = ("select * from state");
        DataTable ds = DAL.tbl_StategetState(query);
        return ds;
    }
 
    public DataTable getcity()
    {
        SqlParameter[] p = new SqlParameter[1];
        p[0] = new SqlParameter("StateId",state);
        DataTable result = DAL.Ret_datatable("selectcity", p);
        return result;
     
    }

}
DAL:

public static DataSet load(String CommandText)
    {

        con = new SqlConnection(connection);
        cmd = new SqlCommand(CommandText, con);
        con.Open();
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter adb = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adb.Fill(ds);
        con.Close();
        return ds;
    }
    public static DataTable tbl_StategetState(String CommandText)
    {

        con = new SqlConnection(connection);
        cmd = new SqlCommand(CommandText, con);
        con.Open();
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        da.Fill(ds);
        dt = ds.Tables[0];
        con.Close();
        return dt;
    }

    public static DataTable Ret_datatable(String CommandText1, SqlParameter[] parameters)
    {
        con = new SqlConnection(connection);
        cmd = new SqlCommand(CommandText1, con);
        con.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        foreach (SqlParameter c in parameters)
        {
            if (c.Value != null)
            {
                cmd.Parameters.Add(c);
            }
        }

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        con.Close();
        return dt;
    }
 

}

Default.aspx:

public partial class _Default : System.Web.UI.Page
{
    BAL b = new BAL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            State();

     
        }
     
     
        load();
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {

        b._UserName = txtusername.Text;
        b._State = Convert.ToInt32(ddlstate.SelectedIndex);
        b._City = Convert.ToInt32(ddlcity.SelectedIndex);
        b._Ip = txtip.Text;
        b._LoginTime = txtlogintime.Text;
        int result = b.tablelogin();
    }
    public void load()
    {
        DataSet ds = b.load();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    public void State()
    {
        DataTable bind = b.getState();
        ddlstate.DataSource = bind;
        ddlstate.DataTextField = bind.Columns["StateName"].ToString();
        ddlstate.DataValueField = bind.Columns["StateId"].ToString();
        ddlstate.DataBind();
        ddlstate.Items.Insert(0, new ListItem("Select", "0"));
     
    }
    public void City()
    {
     
        b._State = Convert.ToInt32(ddlstate.SelectedIndex);
     
        DataTable bind = b.getcity();
        ddlcity.DataSource = bind;
        ddlcity.DataTextField = bind.Columns["CityId"].ToString();
        ddlcity.DataTextField = bind.Columns["CityName"].ToString();
        ddlcity.DataBind();
        ddlcity.Items.Insert(0, new ListItem("select", "0"));
    }
    protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
    {

        City();
   
    }

}
Design gridview bind:


  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField HeaderText="UserName">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%#Bind("UserName") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="State">
    <ItemTemplate>
        <asp:Label ID="Label2" runat="server" Text='<%#Bind("State") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField >
    <asp:TemplateField HeaderText="City">
    <ItemTemplate>
        <asp:Label ID="Label3" runat="server" Text='<%#Bind("City") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Ip">
    <ItemTemplate>
        <asp:Label ID="Label4" runat="server" Text='<%#Bind("Ip") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="LoginTime">
    <ItemTemplate>
        <asp:Label ID="Label5" runat="server" Text='<%#Bind("LoginTime") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
 
    </asp:GridView>
stored procedure:

ALTER procedure [dbo].[selectcity]
@StateId int
as
begin
select * from  dbo.city where StateId=@StateId
end