using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text.RegularExpressions; using System.Data.SqlClient; using System.Xml.Linq; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Configuration; namespace WebApplication1 { public partial class Page2 : System.Web.UI.Page { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["newConnectionString"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "select * from new"; cmd.Connection = conn; conn.Open(); } protected void ButtonSubmit_Click(object sender, EventArgs e) { try { if (validate()) { } } catch(Exception ex) { Response.Write("error:" + ex.ToString()); } conn.Open(); string ins = "Insert into page2 [Name,City,State,Phone,Salary,Age,Gender]values(@Name,@City,@State,@Phone,@Salary,@Age,@Gender)"; SqlCommand cmd= new SqlCommand(ins,conn); cmd.Parameters.AddWithValue("@Name",TextBoxName.Text); cmd.Parameters.AddWithValue("@City",TextBoxCity.Text); cmd.Parameters.AddWithValue("@State",TextBoxState.Text); cmd.Parameters.AddWithValue("@Phone",TextBoxPhone.Text); cmd.Parameters.AddWithValue("@Salary",TextBoxSalary.Text); cmd.Parameters.AddWithValue("@Age",TextBoxAge.Text); cmd.Parameters.AddWithValue("@Gender",DropDownListGender.Text); conn.Close(); Error_Label.Text = "Insertion successfull"; } private bool validate() { Error_Label.Text = string.Empty; if (TextBoxName.Text == "") { Error_Label.Text="enter name first"; return false; } //City Check if (TextBoxCity.Text == "") { Error_Label.Text = "enter City"; return false; } else { string pattern = "^[A-Za-z]+$"; if (!Regex.IsMatch(TextBoxCity.Text, pattern)) { Error_Label.Text = "City should contain only char values"; } } //Checking State if (TextBoxState.Text == "") { Error_Label.Text = "enter State"; return false; } else { string pattern = "^[A-Za-z]+$"; if (!Regex.IsMatch(TextBoxState.Text, pattern)) { Error_Label.Text = "State should contain only char values"; } //int outAge = 0; //bool isValidNumeric = int.TryParse(TextBoxState.Text, out outAge); //if (isValidNumeric) //{ // Error_Label.Text = "accept only characters"; //} //else //{ // Error_Label.Text = "Accept only char"; //} } //checking phone no. if (TextBoxPhone.Text == "") { Error_Label.Text="enter Phone no."; return false; } else { if (TextBoxPhone.Text.Length == 10) { int outAge = 0; bool isValidNumeric = int.TryParse(TextBoxPhone.Text, out outAge); if (isValidNumeric) { //<0 >9891283112 show error if (outAge < 0) { Error_Label.Text = "value cant be negative"; } } else { Error_Label.Text = "Pl. insert valid mobile number only."; } } else { Error_Label.Text = "MObile No. must be 10 digits long"; } } //checking salary if (TextBoxSalary.Text == "") { Error_Label.Text="enter Salary"; return false; } else { int outAge = 0; bool isValidNumeric = int.TryParse(TextBoxSalary.Text, out outAge); if (isValidNumeric) { //<0 >9891283112 show error if (outAge <= 0) { Error_Label.Text = "Salary cant be negative or Zero"; } } else { Error_Label.Text = "Pl. insert valid Salary only."; } } //CHECKING AGE if (TextBoxAge.Text == "") { Error_Label.Text = "Age"; return false; } else { if (TextBoxAge.Text.Length == 2) { int outAge = 0; bool isValidNumeric = int.TryParse(TextBoxAge.Text, out outAge); if (isValidNumeric) { //<0 >9891283112 show error if (outAge <=0) { Error_Label.Text = "value cant be negative"; } } else { Error_Label.Text = "Pl. insert valid Age."; } } else { Error_Label.Text = "Age is limited upto 2 decimals only "; } } //CHECKING GENDER if (DropDownListGender.Text == "") { Error_Label.Text = "Please Select a Gender "; return false; } else return true; } } }
INSERT
INSERT INTO tablename (col1, ...) VALUES ( val1, ...)
Insert into page2 (Name,City,[State],Phone,Salary,Age,Gender) values (@Name,@City,@State,@Phone,@Salary,@Age,@Gender)
()
[]
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)