//Take one text box and call the function obblur method........
asp:TextBox ID="txt" runat="server" onblur="ValidateCCNum(this);">
//.js File
function ValidateCCNum(ccNum)
{
var ccno = ccNum.value;
if(ccno == "")
{
return true;
}
if(isNaN(ccno))
{
alert("Credit card number should be numeric");
ccNum.value="";
ccNum.focus();
return false;
}
if(ccno.length < 14 || ccno.length >18)
{
alert("Credit card number should have 14-18 digits length");
ccNum.value="";
ccNum.focus();
return false;
}
return true;
}
Tuesday, April 13, 2010
Monday, April 12, 2010
Deleted Trigger
//titleauthor is foreigntable
//titles Primary Table......When you delete data from Primary automatically delete data from foreign table.....
//title_id is a Unique key in both table..
//deleted is a magic Table where delete data is stored...
create trigger delcascadetrig
on titleauthor
for delete
as
delete delcascadetrig
from titles, deleted
where titles.title_id = deleted.title_id
//titles Primary Table......When you delete data from Primary automatically delete data from foreign table.....
//title_id is a Unique key in both table..
//deleted is a magic Table where delete data is stored...
create trigger delcascadetrig
on titleauthor
for delete
as
delete delcascadetrig
from titles, deleted
where titles.title_id = deleted.title_id
Labels:
Deleted Trigger
Saturday, April 3, 2010
Bind GridView with Datatable
Add a GridView control, two lable controls, three textbox controls and button control in the page
Create datatable structure.
private DataTable CreateDataTable()
{
DataTable myDataTable = new DataTable();
DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "id";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "username";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "firstname";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "lastname";
myDataTable.Columns.Add(myDataColumn);
return myDataTable;
}
Insert data into datatable.
private void AddDataToTable(string username,string firstname,string lastname,DataTable myTable)
{
DataRow row;
row = myTable.NewRow();
row["id"] = Guid.NewGuid().ToString();
row["username"] = username;
row["firstname"] = firstname;
row["lastname"] = lastname;
myTable.Rows.Add(row);
}
//Add data to datatable which we have created
protected void btnAdd_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Trim() == "")
{
this.lblTips.Text = "You must fill a username.";
return;
}
else
{
AddDataToTable(parameter);
this.txtFirstName.Text = "";
this.txtLastName.Text = "";
this.txtUserName.Text = "";
this.lblTips.Text = "";
}
}
// Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable myDt = new DataTable();
myDt = CreateDataTable();
this.GridView1.DataSource = myDt
this.GridView1.DataBind();
}
}
Create datatable structure.
private DataTable CreateDataTable()
{
DataTable myDataTable = new DataTable();
DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "id";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "username";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "firstname";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "lastname";
myDataTable.Columns.Add(myDataColumn);
return myDataTable;
}
Insert data into datatable.
private void AddDataToTable(string username,string firstname,string lastname,DataTable myTable)
{
DataRow row;
row = myTable.NewRow();
row["id"] = Guid.NewGuid().ToString();
row["username"] = username;
row["firstname"] = firstname;
row["lastname"] = lastname;
myTable.Rows.Add(row);
}
//Add data to datatable which we have created
protected void btnAdd_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Trim() == "")
{
this.lblTips.Text = "You must fill a username.";
return;
}
else
{
AddDataToTable(parameter);
this.txtFirstName.Text = "";
this.txtLastName.Text = "";
this.txtUserName.Text = "";
this.lblTips.Text = "";
}
}
// Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable myDt = new DataTable();
myDt = CreateDataTable();
this.GridView1.DataSource = myDt
this.GridView1.DataBind();
}
}
Labels:
Bind GridView with Datatable
Saturday, March 27, 2010
Update , Delete inside the grid view
//Take one Grid View and Bind all Column and take column inside templatefiels inside ItemTemplate and EditItemTemplate ALSO FOR Generate text box for updation
I have remove starting tag from the Html Tag because blog can't take Html field like Starting tag .....
asp:Label ID="lblId" runat="server" Visible="false" Text='%# Bind("Id") %>'>/asp:Label>
//Web.config file
add name="Master" connectionString ="Data Source=.;user id=; password=; database=Master; "/>
//aspx.cs
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Master"].ConnectionString);
SqlDataAdapter adp;
DataSet ds; SqlCommand cmd;
void fillgridview()
{
adp = new SqlDataAdapter("Select a.id,a.Name,a.Age,a.RollNo,b.Subject from student a join subject b on a.subjectid=b.subjectid", con);
ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
else { }
ds.Dispose();
adp = null;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillgridview();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lbnlid = (Label)GridView1.Rows[e.RowIndex].FindControl("lblId");
con.Open();
cmd = new SqlCommand("delete from student where id= " + lbnlid.Text + " ", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
fillgridview();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
fillgridview();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
fillgridview();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity");
Label lbnlid = (Label)GridView1.Rows[e.RowIndex].FindControl("lblId");
con.Open();
cmd = new SqlCommand("update student set Name= '"+txtName.Text +"' where id= "+lbnlid.Text+" ", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
fillgridview();
}
I have remove starting tag from the Html Tag because blog can't take Html field like Starting tag .....
asp:Label ID="lblId" runat="server" Visible="false" Text='%# Bind("Id") %>'>/asp:Label>
//Web.config file
add name="Master" connectionString ="Data Source=.;user id=; password=; database=Master; "/>
//aspx.cs
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Master"].ConnectionString);
SqlDataAdapter adp;
DataSet ds; SqlCommand cmd;
void fillgridview()
{
adp = new SqlDataAdapter("Select a.id,a.Name,a.Age,a.RollNo,b.Subject from student a join subject b on a.subjectid=b.subjectid", con);
ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
else { }
ds.Dispose();
adp = null;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillgridview();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lbnlid = (Label)GridView1.Rows[e.RowIndex].FindControl("lblId");
con.Open();
cmd = new SqlCommand("delete from student where id= " + lbnlid.Text + " ", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
fillgridview();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
fillgridview();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
fillgridview();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity");
Label lbnlid = (Label)GridView1.Rows[e.RowIndex].FindControl("lblId");
con.Open();
cmd = new SqlCommand("update student set Name= '"+txtName.Text +"' where id= "+lbnlid.Text+" ", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
fillgridview();
}
Labels:
Delete inside the grid view,
Update
Disable Browser back Button in asp.net
//Make one .js file
function goNewWin()
{
window.open("default2.aspx",'TheNewpop','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');
self.close()
}
//Call that function and give the page name there which page you don't want browser back button....
function goNewWin()
{
window.open("default2.aspx",'TheNewpop','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');
self.close()
}
//Call that function and give the page name there which page you don't want browser back button....
Gridview sorting,paging
//Take one Grid View and Bind all the column through
//hdnSort is a Hidden field take one hidden field also.
asp:TemplateField
ItemTemplate and give the sort expression also and paging and sorting also true the property of GridView...
//Web.config file
add name="Master1" connectionString ="Data Source=.;user id=; password=; database=Master; "/>
//in aspx.cs
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Master1"].ConnectionString);
private void bindData(string sorting)
{
DataSet ds;
SqlDataAdapter da;
string sql = null;
ds = new DataSet();
da = new SqlDataAdapter();
try
{
con.Open();
}
catch (Exception ex)
{
}
sql = "Select student_id,last_name,first_name,Score from Student order by " + sorting;
da.SelectCommand = new SqlCommand(sql, con);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
if (ds.Tables.Count == 0)
{
//lblMessage.Text = "No record found";
}
}
ds = null;
da = null;
try { con.Close(); }
catch (Exception ex) { }
}
//call Bind Method where you bind the Gridview perform all operation
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindData("student_id");
}
}
//for sorting
private string ConvertSortDirectiontoSql(SortDirection sort)
{
string newSortDirection = string.Empty;
switch (sort)
{
case SortDirection.Ascending:
newSortDirection = "Asc";
hdnSort.Value = "Asc";
break;
case SortDirection.Descending:
newSortDirection = "Desc";
hdnSort.Value = "Desc";
break;
}
return newSortDirection;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//hdnSort is a Hidden field take one hidden field also.
if (hdnSort.Value.Length == 0 || hdnSort.Value == "Desc")
e.SortDirection = SortDirection.Ascending;
else
e.SortDirection = SortDirection.Descending;
string sorting = e.SortExpression + " " + ConvertSortDirectiontoSql(e.SortDirection);
if (hdnSort.Value.Length == 0)
hdnSort.Value = "Desc";
bindData(sorting);
}
//for paging
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindData("student_id desc");
}
//hdnSort is a Hidden field take one hidden field also.
asp:TemplateField
ItemTemplate and give the sort expression also and paging and sorting also true the property of GridView...
//Web.config file
add name="Master1" connectionString ="Data Source=.;user id=; password=; database=Master; "/>
//in aspx.cs
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Master1"].ConnectionString);
private void bindData(string sorting)
{
DataSet ds;
SqlDataAdapter da;
string sql = null;
ds = new DataSet();
da = new SqlDataAdapter();
try
{
con.Open();
}
catch (Exception ex)
{
}
sql = "Select student_id,last_name,first_name,Score from Student order by " + sorting;
da.SelectCommand = new SqlCommand(sql, con);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
if (ds.Tables.Count == 0)
{
//lblMessage.Text = "No record found";
}
}
ds = null;
da = null;
try { con.Close(); }
catch (Exception ex) { }
}
//call Bind Method where you bind the Gridview perform all operation
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindData("student_id");
}
}
//for sorting
private string ConvertSortDirectiontoSql(SortDirection sort)
{
string newSortDirection = string.Empty;
switch (sort)
{
case SortDirection.Ascending:
newSortDirection = "Asc";
hdnSort.Value = "Asc";
break;
case SortDirection.Descending:
newSortDirection = "Desc";
hdnSort.Value = "Desc";
break;
}
return newSortDirection;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//hdnSort is a Hidden field take one hidden field also.
if (hdnSort.Value.Length == 0 || hdnSort.Value == "Desc")
e.SortDirection = SortDirection.Ascending;
else
e.SortDirection = SortDirection.Descending;
string sorting = e.SortExpression + " " + ConvertSortDirectiontoSql(e.SortDirection);
if (hdnSort.Value.Length == 0)
hdnSort.Value = "Desc";
bindData(sorting);
}
//for paging
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindData("student_id desc");
}
Fill GridView
//Web.Config File Connection String
add name="Master1" connectionString ="Data Source=.;user id=; password=; database=Master; "/>
//aspx.cs
//Bind Function and call that function where you want..
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Master1"].ConnectionString);
private void bindData()
{
DataSet ds;
SqlDataAdapter da;
string sql = null;
ds = new DataSet();
da = new SqlDataAdapter();
try
{
con.Open();
}
catch (Exception ex)
{
}
sql = "Select student_id,last_name,first_name,Score from Student ;
da.SelectCommand = new SqlCommand(sql, con);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
if (ds.Tables.Count == 0)
{
//lblMessage.Text = "No record found";
}
}
ds = null;
da = null;
try { con.Close(); }
catch (Exception ex) { }
}
add name="Master1" connectionString ="Data Source=.;user id=; password=; database=Master; "/>
//aspx.cs
//Bind Function and call that function where you want..
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Master1"].ConnectionString);
private void bindData()
{
DataSet ds;
SqlDataAdapter da;
string sql = null;
ds = new DataSet();
da = new SqlDataAdapter();
try
{
con.Open();
}
catch (Exception ex)
{
}
sql = "Select student_id,last_name,first_name,Score from Student ;
da.SelectCommand = new SqlCommand(sql, con);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
if (ds.Tables.Count == 0)
{
//lblMessage.Text = "No record found";
}
}
ds = null;
da = null;
try { con.Close(); }
catch (Exception ex) { }
}
Subscribe to:
Posts (Atom)