Archive for February 2017
web.config - connection string
web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<connectionStrings>
<add name="BiggerrConnection" connectionString="Data Source=ADMIN\MSSQLSERVERNEW;Initial Catalog=Biggerr;Integrated Security=True;" />
</connectionStrings>
ADO.NET DropDownlist binding
SQL Queries
create Table Tbl_Category (CategoryId bigint Primary key identity, CategoryName varchar(100),CreatedTime datetime)
insert Tbl_Category values('Electronics',getdate())
insert Tbl_Category values('Home Appliances',getdate())
insert Tbl_Category values('Books',getdate())
insert Tbl_Category values('Fashion and Life Style',getdate())
select * from Tbl_Category
create Table Tbl_SubCategory (SubcategoryId bigint Primary key identity,SubCatCategoryId bigint foreign key references Tbl_Category(CategoryId),
SubcategoryName varchar(100),createdtime datetime)
insert Tbl_SubCategory values ('1','Air Conditioners',getdate())
insert Tbl_SubCategory values ('1','LEDs',getdate())
insert Tbl_SubCategory values ('1','Mobiles',getdate())
insert Tbl_SubCategory values ('1','Others',getdate())
insert Tbl_SubCategory values ('2','Mixer',getdate())
insert Tbl_SubCategory values ('2','Grinder',getdate())
insert Tbl_SubCategory values ('2','Microwave',getdate())
insert Tbl_SubCategory values ('2','Fridge',getdate())
select * from Tbl_SubCategory
ASP.NET Source Code
<asp:DropDownList ID="DDLCatagories" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDLCatagories_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DDLSubCatagories" runat="server">
</asp:DropDownList>
ASP.NET C# Code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace BIGGERR.Customer
{
public partial class CustomerProducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DisplayCategory();
}
}
public void DisplayCategory()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BiggerrConnection"].ConnectionString);
string s = "select * from Tbl_Category";
SqlCommand cmd = new SqlCommand(s, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DDLCatagories.DataSource = ds;
DDLCatagories.DataValueField = "CategoryId";
DDLCatagories.DataTextField = "CategoryName";
DDLCatagories.DataBind();
}
protected void DDLCatagories_SelectedIndexChanged(object sender, EventArgs e)
{
DisplaySubCategory();
}
public void DisplaySubCategory()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BiggerrConnection"].ConnectionString);
string s = "select * from Tbl_SubCategory where SubCatCategoryId="+DDLCatagories.SelectedValue;
SqlCommand cmd = new SqlCommand(s, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DDLSubCatagories.DataSource = ds;
DDLSubCatagories.DataValueField = "SubcategoryId";
DDLSubCatagories.DataTextField = "SubcategoryName";
DDLSubCatagories.DataBind();
}
}
}
web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<connectionStrings>
<add name="BiggerrConnection" connectionString="Data Source=ADMIN\MSSQLSERVERNEW;Initial Catalog=Biggerr;Integrated Security=True;" />
</connectionStrings>
</configuration>
GridView Edit Update Delete
SQL Queries
create table Tbl_Admin(Aid int identity primary key, Username varchar(50),Pwd varchar(50),PhoneNumber varchar(20),createdtime datetime)
insert into Tbl_Admin values('sudhir','sud',999898898,getdate())
insert into Tbl_Admin values('raj','raj123',999898897,getdate())
insert into Tbl_Admin values('gopal','gopal123',999898896,getdate())
insert into Tbl_Admin values('sai','sai123',999898895,getdate())
select * from Tbl_Admin
ASP.NET Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Aid"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="Aid" HeaderText="Aid" />
<asp:BoundField DataField="Username" HeaderText="Username" />
<asp:BoundField DataField="Pwd" HeaderText="Password" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
ASP.NET Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Aid"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="Aid" HeaderText="Aid" />
<asp:BoundField DataField="Username" HeaderText="Username" />
<asp:BoundField DataField="Pwd" HeaderText="Password" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
C#.NET Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication3
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//only for first time
GridFill();
}
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=JobPortal;Integrated Security=True");
public void GridFill()
{
//GridView Fill
SqlCommand cmd = new SqlCommand("select * from Tbl_Admin", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
// Label lbldeleteid = (Label)row.FindControl("lblID");
con.Open();
SqlCommand cmd = new SqlCommand("delete FROM tbl_admin where aid='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
cmd.ExecuteNonQuery();
con.Close();
GridFill();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridFill();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int aid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
// Label lblID = (Label)row.FindControl("lblID");
//TextBox txtname=(TextBox)gr.cell[].control[];
TextBox textName = (TextBox)row.Cells[2].Controls[0];
TextBox textpwd = (TextBox)row.Cells[3].Controls[0];
//to convert textboxes to labels in gridview
GridView1.EditIndex = -1;
con.Open();
SqlCommand cmd = new SqlCommand("update tbl_admin set username='" + textName.Text + "',pwd='" + textpwd.Text + "' where aid='" + aid + "'", con);
cmd.ExecuteNonQuery();
con.Close();
GridFill();
}
}
}