ASP.NET

ASP.NET 

1. How to create Grid view Dynamic In Asp.net & C# ?
.cs 
 protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from Customers ", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView GridView1 = new GridView();
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        PanelGridView.Controls.Add(GridView1);
    }
.aspx
<asp:Panel ID="PanelGridView" runat="server" Height="273px" Width="1109px"></asp:Panel>
2. How to create Cascading Dropdown  In Asp.net & C# ?

SqlConnection con
= new SqlConnection("userid=sa;password=123;database=master;server=dell");
   //Page Load
    protected void Page_Load(object sender, EventArgs e)
    {
        {
            if (!IsPostBack)
            {
                BindContrydropdown();
            }
        }
    }
  //Country Dropdown list Event//Auto postback ==true
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int CountryID = Convert.ToInt32(DropDownList1.SelectedValue);
        con.Open();
SqlCommand cmd = new SqlCommand("select * from StateTable where CountryID=" + CountryID, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        DropDownList2.DataSource = ds;
        DropDownList2.DataTextField = "StateName";
        DropDownList2.DataValueField = "StateID";
        DropDownList2.DataBind();
        DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));
        if (DropDownList2.SelectedValue == "0")
        {
            DropDownList3.Items.Clear();
            DropDownList3.Items.Insert(0, new ListItem("--Select--", "0"));
        }
        }

    //State Dropdown list Event//Auto postback ==true
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        {
            int StateID = Convert.ToInt32(DropDownList2.SelectedValue);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from RegionTable where StateID=" + StateID, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            DropDownList3.DataSource = ds;
            DropDownList3.DataTextField = "RegionName";
            DropDownList3.DataValueField = "RegionID";
            DropDownList3.DataBind();
            DropDownList3.Items.Insert(0, new ListItem("--Select--", "0"));

        }
    }

    //Bind dropdown method
    protected void BindContrydropdown()
    {
        //conenction path for database
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from CountryTable", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
     
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "CountryName";
        DropDownList1.DataValueField = "CountryID";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--Select--", "0"));
        DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));
        DropDownList3.Items.Insert(0, new ListItem("--Select--", "0"));
    }
}

3. How to Retrive Data in to TextBox from Database  In Asp.net & C# ?

Connection In Web.cofig File


<appSettings>
<add key="dbconn" 
value="user id=sa;
password=123;
server=chanakyadell;
database=master"/>
</appSettings>

 .cs File
string s = System.Configuration.ConfigurationManager.AppSettings["dbconn"].ToString();

  protected void Page_Load(object sender, EventArgs e)
    {
        fillgrid();
    }
  protected void Button1_Click1(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(s);
        SqlCommand cmd = new SqlCommand("select * from userDetails where Userid='" + TextBox1.Text + "'", con);
       
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@Id", TextBox1.Text);
        cmd.Connection = con;
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();

        da.Fill(ds);
      
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
      
        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
 //TextBox1.Text = dr["Userid"].ToString();

            TextBox4.Text = dr["UserName"].ToString();

            TextBox3.Text = dr["FirstName"].ToString();

            TextBox5.Text = dr["LastName"].ToString();
            TextBox6.Text = dr["Location"].ToString();
            //fillgrid();
        }
  con.Close();
        dr.Close();

    }
 private void fillgrid()
        {
            SqlConnection con = new SqlConnection(s);
            SqlDataAdapter da = new SqlDataAdapter
              ("select * from userDetails", con);

            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
4. Grid Crud Operation  In Asp.net & C# ?
.cs file

SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["constr"].ToString());
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        BindEmployeeDetails();
        }
    }
    protected void BindEmployeeDetails()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from Employee_Details", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            int columncount = gvDetails.Rows[0].Cells.Count;
            gvDetails.Rows[0].Cells.Clear();
            gvDetails.Rows[0].Cells.Add(new TableCell());
            gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvDetails.Rows[0].Cells[0].Text = "No Records Found";
        }
    }
    protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDetails.EditIndex = -1;
        BindEmployeeDetails();
    }
    protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox txtUsrname = (TextBox)gvDetails.FooterRow.FindControl("txtftrusrname");
            TextBox txtCity = (TextBox)gvDetails.FooterRow.FindControl("txtftrcity");
            TextBox txtDesgnation = (TextBox)gvDetails.FooterRow.FindControl("txtftrDesignation");
            con.Open();
            SqlCommand cmd =
            new SqlCommand(
            "insert into Employee_Details(UserName,City,Designation) values('" + txtUsrname.Text + "','" +
            txtCity.Text + "','" + txtDesgnation.Text + "')", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindEmployeeDetails();
                lblresult.ForeColor = Color.Green;
                lblresult.Text = txtUsrname.Text + " Details inserted successfully";
            }
            else
            {
                lblresult.ForeColor = Color.Red;
                lblresult.Text = txtUsrname.Text + " Details not inserted";
            }
        }
    }

    //delete
    protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
        string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from Employee_Details where UserId=" + userid, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            BindEmployeeDetails();
            lblresult.ForeColor = Color.Red;
            lblresult.Text = username + " details deleted successfully";
        }
    }

    //update
    protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDetails.EditIndex = e.NewEditIndex;
        BindEmployeeDetails();
    }

    //Update
    protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
        TextBox txtcity = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtcity");
        TextBox txtDesignation = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDesg");
        con.Open();
        SqlCommand cmd = new SqlCommand("update Employee_Details set City='" + txtcity.Text + "',Designation='" + txtDesignation.Text + "' where UserId=" + userid, con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblresult.ForeColor = Color.Green;
        lblresult.Text = username + " Details Updated successfully";
        gvDetails.EditIndex = -1;
        BindEmployeeDetails();
    }
 protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        BindEmployeeDetails();

        gvDetails.PageIndex = e.NewPageIndex;
        gvDetails.DataBind();
    }
.aspx file



<form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails" DataKeyNames="UserId,UserName" runat="server"
AutoGenerateColumns="false" CssClass="Gridview" HeaderStyle-BackColor="#61A6F8"
ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"
onrowcancelingedit="gvDetails_RowCancelingEdit"
onrowdeleting="gvDetails_RowDeleting" onrowediting="gvDetails_RowEditing"
onrowupdating="gvDetails_RowUpdating"
onrowcommand="gvDetails_RowCommand">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.png" ToolTip="Update" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.png" ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/edit.png" ToolTip="Edit" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/Images/delete.png" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.png" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName">
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("Username") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("UserName") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrusrname" runat="server"/>
<asp:RequiredFieldValidator ID="rfvusername" runat="server" ControlToValidate="txtftrusrname" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="txtcity" runat="server" Text='<%#Eval("City") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%#Eval("City") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrcity" runat="server"/>
<asp:RequiredFieldValidator ID="rfvcity" runat="server" ControlToValidate="txtftrcity" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<EditItemTemplate>
<asp:TextBox ID="txtDesg" runat="server" Text='<%#Eval("Designation") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDesg" runat="server" Text='<%#Eval("Designation") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrDesignation" runat="server"/>
<asp:RequiredFieldValidator ID="rfvdesignation" runat="server" ControlToValidate="txtftrDesignation" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<asp:Label ID="lblresult" runat="server"></asp:Label>
</div>
</form>

database

create table Employee_Details(UserId int identity,
UserName varchar(20)
,city varchar(10)
,Designation varchar(50))

NORMAL LOGIN

 string s = "select count(*) from login where username='" + TextBox1.Text + "' and pwd='" + TextBox2.Text + "',";
        SqlCommand cmd = new SqlCommand(s,con);
        con.Open();
        int n = (int)cmd.ExecuteScalar();
        con.Close();

        if (n == 1)
            Server.Transfer("Welcome.aspx");
        else
            Response.Redirect("Invalid");


Insert page

Sqlconnection con=new  sqlConnection(----);
string s="insert into login values("+textBox1.text+",'"+textBox2.text+")";
con.open();
sqlcommand cmd=new sqlccommand(s,con);
cmd.executenonquery();
con.close();
server.transfer("Welcome.aspx");


No comments:

Post a Comment

Search This Blog