The following Shows the Client Side Validation Web Form Using Java Script
Source Code:
Java Script:
function Submit1_onclick() {
var t1,t2,t3,t4,t5;
t1 = document.getElementById("TextBox1");
if(t1.value == "")
{
alert("enter the Name ");
document.getElementById("TextBox1").focus();
//t1.value=" " ;
return false;
}
t2 = document.getElementById("TextBox2");
if(t2.value == "")
{
alert("enter the Reg Number");
document.getElementById("TextBox2").focus();
return false;
}
t3 = document.getElementById("TextBox3");
if(t3.value == "")
{
alert("enter the Pass Word");
document.getElementById("TextBox3").focus();
return false;
}
t4 = document.getElementById("TextBox4");
if(t4.value == "" )
{
alert("enter the Confirm Pass Word");
document.getElementById("TextBox4").focus();
return false;
}
if( t3.value != t4.value)
{
alert (" Pass Word Mismatch ");
return false;
}
t5 = document.getElementById("TextBox5");
if(t5.value == "")
{
alert("enter the e-mail");
return false;
}
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid = document.getElementById("TextBox5").value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.");
document.getElementById("TextBox5").focus();
return false;
}
t6 = document.getElementById("TextBox6");
if(t6.value == "")
{
alert("enter the Date of Birth");
return false;
}
//var dateform =/(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$/; //mm/dd/yyyy
alert("Please click the Submit Button");
return true;
}
The Default.aspx.cs file is
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// if(!(Page.IsPostBack))
// only executes for the first time of the page load event.. initially alone
//Button1.Attributes.Add("onclick", "Submit1_onclick();");
}
public string name1, password1, email1, dob1;
public int regno1;
SqlCommand cmd;
//submit
protected void Button1_Click(object sender, EventArgs e)
{
string constr, query;
constr = "Data Source=6;Initial Catalog=employee123;User ID=sa;Password=123";
SqlConnection conn = new SqlConnection(constr);
try
{
/*query = "Insert into regform values(@name,@regno,@password,@email,@DOB)";
cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@name",TextBox1.Text);
cmd.Parameters.AddWithValue("@regno",TextBox2.Text);
cmd.Parameters.AddWithValue("@password",TextBox3.Text);
cmd.Parameters.AddWithValue("@email", TextBox5.Text);
cmd.Parameters.AddWithValue("@DOB", TextBox6.Text);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("the value updated successfully");*/
cmd = new SqlCommand("regforms_sp", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text;
cmd.Parameters.Add("@regno", SqlDbType.VarChar).Value = TextBox2.Text;
cmd.Parameters.Add("@passwords", SqlDbType.VarChar).Value = TextBox3.Text;
cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = TextBox5.Text;
cmd.Parameters.Add("@DOB", SqlDbType.VarChar).Value = TextBox6.Text;
conn.Open();
int rows = cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex);
}
}
//display
protected void Button2_Click(object sender, EventArgs e)
{
string constr;
constr = "Data Source=6;Initial Catalog=employee123;User ID=sa;Password=123";
SqlConnection conn = new SqlConnection(constr);
try
{
string sql123 = "select * from regforms ";
SqlDataAdapter da = new SqlDataAdapter(sql123,conn);
DataSet ds = new DataSet();
da.Fill(ds);
int y = 0;
for (int x=1; x < ds.Tables[0].Rows.Count;y++)
{
Response.Write(ds.Tables[0].Rows[x].ItemArray[y].ToString());
if (y == 4)
break;
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
protected void TextBox6_TextChanged(object sender, EventArgs e)
{
}
}
Tuesday, May 11, 2010
XML Manipulation in C# .NET
XML Insert, Update ,Delete Example
Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace xmlupdate
{
public partial class Form1 : Form
{
public static int ii =0;
public static int i = 0;
public static int j = 0;
public static int x = 0;
public static int flag=0;
string[] str1 = new string[20];
XmlTextWriter xwr;
XmlDocument doc = new XmlDocument();
XmlNodeList n1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
xwr = new XmlTextWriter("sample123.xml", null);
xwr.WriteStartDocument();
xwr.WriteStartElement("TasksMain");
xwr.Formatting = Formatting.Indented;
xwr.WriteComment("This is comment Line");
}
//create
private void button1_Click(object sender, EventArgs e)
{
if ((textBox1.Text == "") || (textBox2.Text == "") || (textBox3.Text == ""))
label4.Text = "please enter the values";
else
{
xwr.WriteStartElement("Tasks");
xwr.WriteElementString("name", textBox1.Text);
xwr.WriteElementString("age", textBox2.Text);
xwr.WriteElementString("sex", textBox3.Text);
xwr.WriteEndElement();
label4.Text = "Record created success fully";
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (flag != 1)
{
xwr.WriteEndElement();
xwr.WriteEndDocument();
xwr.Close();
}
}
//display
private void button2_Click(object sender, EventArgs e)
{
//xwr.WriteEndElement();
//xwr.WriteEndDocument();
//xwr.Close();
flag = 1;
doc.Load("sample123.xml");
XmlNodeReader reader = new XmlNodeReader(doc);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Text:
str1[ii] = reader.Value;
ii++;
break;
}
}
for (; i < 3; i++)
{
textBox1.Text = str1[0];
textBox2.Text = str1[1];
textBox3.Text = str1[2];
}
button2.Enabled = false;
reader.Close();
}
//display next
private void button4_Click(object sender, EventArgs e)
{
try
{
textBox1.Text = str1[x];
textBox2.Text = str1[x + 1];
textBox3.Text = str1[x + 2];
x += 3;
}
catch (Exception e1)
{
MessageBox.Show("" + e1);
button2.Enabled = false;
}
}
//append
private void button3_Click(object sender, EventArgs e)
{
// XmlDocument doc1 = new XmlDocument();
//doc1.Load("ooty1.xml");
if ((textBox1.Text == "") || (textBox2.Text == "") || (textBox3.Text == ""))
label4.Text = "please enter the values";
//doc.Load("sample123.xml");
//XmlElement MainTasks = doc.CreateElement("TasksMain22");
XmlElement newTasks = doc.CreateElement("Tasks");
XmlElement newname = doc.CreateElement("name");
newname.InnerText = textBox1.Text;
XmlElement newage = doc.CreateElement("age");
newage.InnerText = textBox2.Text;
XmlElement newgen = doc.CreateElement("sex");
newgen.InnerText = textBox3.Text;
newTasks.AppendChild(newname);
newTasks.AppendChild(newage);
newTasks.AppendChild(newgen);
doc.DocumentElement.AppendChild(newTasks);
doc.Save("sample123.xml");
label4.Text = "Record Updated Successfully";
button9.PerformClick();
//MainTasks.AppendChild(newTasks);
}
//delete
private void button5_Click(object sender, EventArgs e)
{
string match;
match = textBox4.Text;
XmlNodeList nodeList = doc.SelectNodes("TasksMain");
n1 = doc.GetElementsByTagName("Tasks");
int flaggy = 0;
foreach (XmlNode node in n1)
{
if ( match == node.SelectSingleNode("name").InnerText)
{
flaggy = 1;
doc.DocumentElement.RemoveChild(node);
doc.Save("sample123.xml");
MessageBox.Show("the node deleted");
//textBox1.Text = node.SelectSingleNode("name").InnerText;
//textBox2.Text = node.SelectSingleNode("age").InnerText;
//textBox3.Text = node.SelectSingleNode("sex").InnerText;
}
if (flaggy == 1)
break;
}
}
//writing finished
private void button6_Click(object sender, EventArgs e)
{
xwr.WriteEndElement();
xwr.WriteEndDocument();
xwr.Close();
button6.Enabled = false;
button1.Enabled = false;
}
//clear
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
//find..
private void button7_Click(object sender, EventArgs e)
{
XmlNodeList nodeList = doc.SelectNodes("TasksMain");
n1 = doc.GetElementsByTagName("Tasks");
int flaggy = 0;
foreach (XmlNode node in n1)
{
if(textBox4.Text == node.SelectSingleNode("name").InnerText)
{
flaggy = 1;
textBox1.Text = node.SelectSingleNode("name").InnerText;
textBox2.Text = node.SelectSingleNode("age").InnerText;
textBox3.Text = node.SelectSingleNode("sex").InnerText;
}
if (flaggy == 1)
{
break;
}
}
if(flaggy == 0)
MessageBox.Show("Sorry Record not Found");
}
private void button8_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace xmlupdate
{
public partial class Form1 : Form
{
public static int ii =0;
public static int i = 0;
public static int j = 0;
public static int x = 0;
public static int flag=0;
string[] str1 = new string[20];
XmlTextWriter xwr;
XmlDocument doc = new XmlDocument();
XmlNodeList n1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
xwr = new XmlTextWriter("sample123.xml", null);
xwr.WriteStartDocument();
xwr.WriteStartElement("TasksMain");
xwr.Formatting = Formatting.Indented;
xwr.WriteComment("This is comment Line");
}
//create
private void button1_Click(object sender, EventArgs e)
{
if ((textBox1.Text == "") || (textBox2.Text == "") || (textBox3.Text == ""))
label4.Text = "please enter the values";
else
{
xwr.WriteStartElement("Tasks");
xwr.WriteElementString("name", textBox1.Text);
xwr.WriteElementString("age", textBox2.Text);
xwr.WriteElementString("sex", textBox3.Text);
xwr.WriteEndElement();
label4.Text = "Record created success fully";
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (flag != 1)
{
xwr.WriteEndElement();
xwr.WriteEndDocument();
xwr.Close();
}
}
//display
private void button2_Click(object sender, EventArgs e)
{
//xwr.WriteEndElement();
//xwr.WriteEndDocument();
//xwr.Close();
flag = 1;
doc.Load("sample123.xml");
XmlNodeReader reader = new XmlNodeReader(doc);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Text:
str1[ii] = reader.Value;
ii++;
break;
}
}
for (; i < 3; i++)
{
textBox1.Text = str1[0];
textBox2.Text = str1[1];
textBox3.Text = str1[2];
}
button2.Enabled = false;
reader.Close();
}
//display next
private void button4_Click(object sender, EventArgs e)
{
try
{
textBox1.Text = str1[x];
textBox2.Text = str1[x + 1];
textBox3.Text = str1[x + 2];
x += 3;
}
catch (Exception e1)
{
MessageBox.Show("" + e1);
button2.Enabled = false;
}
}
//append
private void button3_Click(object sender, EventArgs e)
{
// XmlDocument doc1 = new XmlDocument();
//doc1.Load("ooty1.xml");
if ((textBox1.Text == "") || (textBox2.Text == "") || (textBox3.Text == ""))
label4.Text = "please enter the values";
//doc.Load("sample123.xml");
//XmlElement MainTasks = doc.CreateElement("TasksMain22");
XmlElement newTasks = doc.CreateElement("Tasks");
XmlElement newname = doc.CreateElement("name");
newname.InnerText = textBox1.Text;
XmlElement newage = doc.CreateElement("age");
newage.InnerText = textBox2.Text;
XmlElement newgen = doc.CreateElement("sex");
newgen.InnerText = textBox3.Text;
newTasks.AppendChild(newname);
newTasks.AppendChild(newage);
newTasks.AppendChild(newgen);
doc.DocumentElement.AppendChild(newTasks);
doc.Save("sample123.xml");
label4.Text = "Record Updated Successfully";
button9.PerformClick();
//MainTasks.AppendChild(newTasks);
}
//delete
private void button5_Click(object sender, EventArgs e)
{
string match;
match = textBox4.Text;
XmlNodeList nodeList = doc.SelectNodes("TasksMain");
n1 = doc.GetElementsByTagName("Tasks");
int flaggy = 0;
foreach (XmlNode node in n1)
{
if ( match == node.SelectSingleNode("name").InnerText)
{
flaggy = 1;
doc.DocumentElement.RemoveChild(node);
doc.Save("sample123.xml");
MessageBox.Show("the node deleted");
//textBox1.Text = node.SelectSingleNode("name").InnerText;
//textBox2.Text = node.SelectSingleNode("age").InnerText;
//textBox3.Text = node.SelectSingleNode("sex").InnerText;
}
if (flaggy == 1)
break;
}
}
//writing finished
private void button6_Click(object sender, EventArgs e)
{
xwr.WriteEndElement();
xwr.WriteEndDocument();
xwr.Close();
button6.Enabled = false;
button1.Enabled = false;
}
//clear
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
//find..
private void button7_Click(object sender, EventArgs e)
{
XmlNodeList nodeList = doc.SelectNodes("TasksMain");
n1 = doc.GetElementsByTagName("Tasks");
int flaggy = 0;
foreach (XmlNode node in n1)
{
if(textBox4.Text == node.SelectSingleNode("name").InnerText)
{
flaggy = 1;
textBox1.Text = node.SelectSingleNode("name").InnerText;
textBox2.Text = node.SelectSingleNode("age").InnerText;
textBox3.Text = node.SelectSingleNode("sex").InnerText;
}
if (flaggy == 1)
{
break;
}
}
if(flaggy == 0)
MessageBox.Show("Sorry Record not Found");
}
private void button8_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Database Connectivity
The following example shows how to connect the C# .net form with the SQL Server 2005
The Source code is given below............................
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
namespace DBConnect
{
public partial class Form1 : Form
{
SqlConnection con;
SqlDataAdapter da;
SqlCommandBuilder cb;
DataSet ds;
int rno = 0;
public Form1()
{
InitializeComponent();
}
private void ShowData()
{
textBox1.Text = ds.Tables[0].Rows[rno][0].ToString();
textBox2.Text = ds.Tables[0].Rows[rno][1].ToString();
textBox3.Text = ds.Tables[0].Rows[rno][2].ToString();
textBox4.Text = ds.Tables[0].Rows[rno][3].ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
con = new SqlConnection("user ID=sa; Password=sasa; Database=employee123");
da = new SqlDataAdapter("Select * From concern", con);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
ds = new DataSet();
da.Fill(ds, "concern");
ShowData();
}
//First
private void button3_Click_1(object sender, EventArgs e)
{
rno = 0;
ShowData();
}
//insert
private void button6_Click_1(object sender, EventArgs e)
{
DataRow dr = ds.Tables[0].NewRow();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text;
dr[2] = textBox3.Text;
dr[3] = textBox4.Text;
ds.Tables[0].Rows.Add(dr);
MessageBox.Show("Inserted");
button3.PerformClick();
}
//update
private void button7_Click_1(object sender, EventArgs e)
{
ds.Tables[0].Rows[rno][0] = textBox1.Text;
ds.Tables[0].Rows[rno][1] = textBox2.Text;
ds.Tables[0].Rows[rno][2] = textBox3.Text;
ds.Tables[0].Rows[rno][3] = textBox4.Text;
MessageBox.Show("Updated");
}
//delete
private void button8_Click_1(object sender, EventArgs e)
{
ds.Tables[0].Rows[rno].Delete();
MessageBox.Show("Deleted");
button3.PerformClick();
}
//Save
private void button9_Click_1(object sender, EventArgs e)
{
cb = new SqlCommandBuilder(da);
da.Update(ds, "concern");
MessageBox.Show("Data Saved to DB");
}
//Find
private void button10_Click_1(object sender, EventArgs e)
{
/* int sno = int.Parse(Interaction .InputBox("Enter Employee No", "Question Message", "", 100, 100));
DataRow dr = ds.Tables[0].Rows.Find(sno);
textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();
textBox3.Text = dr[2].ToString();
textBox4.Text = dr[3].ToString();*/
}
//next
private void button1_Click_1(object sender, EventArgs e)
{
if (rno < ds.Tables[0].Rows.Count - 1)
{
rno++;
ShowData();
}
else
MessageBox.Show("Last Record");
}
//Previous
private void button2_Click_1(object sender, EventArgs e)
{
if (rno > 0)
{
rno -= 1;
ShowData();
}
else
MessageBox.Show("First Record");
}
//last
private void button4_Click_1(object sender, EventArgs e)
{
rno = ds.Tables[0].Rows.Count - 1;
ShowData();
}
private void button5_Click_1(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
}
}
}
The Source code is given below............................
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
namespace DBConnect
{
public partial class Form1 : Form
{
SqlConnection con;
SqlDataAdapter da;
SqlCommandBuilder cb;
DataSet ds;
int rno = 0;
public Form1()
{
InitializeComponent();
}
private void ShowData()
{
textBox1.Text = ds.Tables[0].Rows[rno][0].ToString();
textBox2.Text = ds.Tables[0].Rows[rno][1].ToString();
textBox3.Text = ds.Tables[0].Rows[rno][2].ToString();
textBox4.Text = ds.Tables[0].Rows[rno][3].ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
con = new SqlConnection("user ID=sa; Password=sasa; Database=employee123");
da = new SqlDataAdapter("Select * From concern", con);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
ds = new DataSet();
da.Fill(ds, "concern");
ShowData();
}
//First
private void button3_Click_1(object sender, EventArgs e)
{
rno = 0;
ShowData();
}
//insert
private void button6_Click_1(object sender, EventArgs e)
{
DataRow dr = ds.Tables[0].NewRow();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text;
dr[2] = textBox3.Text;
dr[3] = textBox4.Text;
ds.Tables[0].Rows.Add(dr);
MessageBox.Show("Inserted");
button3.PerformClick();
}
//update
private void button7_Click_1(object sender, EventArgs e)
{
ds.Tables[0].Rows[rno][0] = textBox1.Text;
ds.Tables[0].Rows[rno][1] = textBox2.Text;
ds.Tables[0].Rows[rno][2] = textBox3.Text;
ds.Tables[0].Rows[rno][3] = textBox4.Text;
MessageBox.Show("Updated");
}
//delete
private void button8_Click_1(object sender, EventArgs e)
{
ds.Tables[0].Rows[rno].Delete();
MessageBox.Show("Deleted");
button3.PerformClick();
}
//Save
private void button9_Click_1(object sender, EventArgs e)
{
cb = new SqlCommandBuilder(da);
da.Update(ds, "concern");
MessageBox.Show("Data Saved to DB");
}
//Find
private void button10_Click_1(object sender, EventArgs e)
{
/* int sno = int.Parse(Interaction .InputBox("Enter Employee No", "Question Message", "", 100, 100));
DataRow dr = ds.Tables[0].Rows.Find(sno);
textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();
textBox3.Text = dr[2].ToString();
textBox4.Text = dr[3].ToString();*/
}
//next
private void button1_Click_1(object sender, EventArgs e)
{
if (rno < ds.Tables[0].Rows.Count - 1)
{
rno++;
ShowData();
}
else
MessageBox.Show("Last Record");
}
//Previous
private void button2_Click_1(object sender, EventArgs e)
{
if (rno > 0)
{
rno -= 1;
ShowData();
}
else
MessageBox.Show("First Record");
}
//last
private void button4_Click_1(object sender, EventArgs e)
{
rno = ds.Tables[0].Rows.Count - 1;
ShowData();
}
private void button5_Click_1(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
}
}
}
Subscribe to:
Posts (Atom)