Click here to Skip to main content
15,914,237 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
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;
        }
       
        
    }
}
Posted
Updated 21-May-15 20:36pm
v4
Comments
Mathi Mani 22-May-15 2:26am    
This is a lot of code and it is poorly formatted. Please improve your question. Sharing the code where you get the exception should be good. Also check the syntax of your insert query. It seems to be the cause.

INSERT follows this syntax :
SQL
INSERT INTO tablename (col1, ...) VALUES ( val1, ...)

it does not use square brackets [ ]
 
Share this answer
 
Comments
Member 11586797 22-May-15 2:29am    
Must declare the scalar variable "@Name". now sql server giving this error
Thanks7872 22-May-15 2:45am    
Do you have access to google.com?
your sql statement should be:
SQL
Insert into page2 (Name,City,[State],Phone,Salary,Age,Gender) values (@Name,@City,@State,@Phone,@Salary,@Age,@Gender)

Note that proper use of () and [], read when to use [] In this answer
 
Share this answer
 
v2
Comments
Member 11586797 22-May-15 2:39am    
still same error--Must declare the scalar variable "@Name"
DamithSL 22-May-15 2:45am    
in which line? you are not executing command after set parameters. call cmd.ExecuteNonQuery(); after you set parameter values and before closing connection
Member 11586797 22-May-15 2:49am    
it is done already
DamithSL 22-May-15 3:01am    
where? I can't find it in the given code
Member 11586797 22-May-15 3:06am    
actually the values are getting into the database , but when i m this string query into the sql server it is showing this message...Incorrect syntax near 'Name'.
hi,
just type your Query in SQL and execute it.
the query will b wrong.
Insert into page2 [Name,City,State,Phone,Salary,Age,Gender]values(@Name,@City,@State,@Phone,@Salary,@Age,@Gender ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900