Click here to Skip to main content
14,493,630 members

error in registartion page while connecting to database

asaboor12 asked:

Open original thread
em getting the following error while inserting to database
the name "Name " does not exist in current context
the name"Father Name "does not exist in current context
and so on for all fields

please help me out

here is the code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class registration : System.Web.UI.Page
    public string GetConnectionString()
        //we will set up the configuration which will call our 
        //web.config file to provide the database details because 
        //in configuration file we have created the <connectionStrings>
        //in the process we draged and droped. It creates automatically.
        //We normally put the database details in web.config file or
        //machine.config file because it is very sensitive information
        //usually there IP address of remote database, passwords and
        //user names are stored.
        return System.Configuration.ConfigurationManager.ConnectionStrings
        //in above line "onlineapplicationformConnectionString1" is 
        //our configuration name which is inside the web.config file.
    private void execution(string  Name, string FatherName, string Email, string LandLineNo, string MobileNo, string Country, string Gender)
        //In above line we declaring different variables same as backend
        SqlConnection conn = new SqlConnection(GetConnectionString());
        //In above line we are calling connection 
        //string function which is defined already on top
        string sql = "INSERT INTO registration (Name, Father Name, Email, Land Line No, Mobile No, Country,Gender) VALUES "
        + " (@Name, @Father Name, @Email, @Land Line No, @Mobile No, @Country, @Gender)";
        //In above lines we are just storing the sql commands which 
        //will insert value in onlineapplication named table, 
        //using variable named sql.
            SqlCommand cmd = new SqlCommand(sql, conn);
            //In above lines we are opening the connection to work and
            //also storing connection name and sql command in cmd variable
            //which has 'SqlCommand' type.
            SqlParameter[] pram = new SqlParameter[7];
            //In above lines we are defining 7 sql parameters will be use
            //In below lines we will not disscuss about id column
            pram[0] = new SqlParameter("@Name", SqlDbType.VarChar, 50);
            pram[1] = new SqlParameter("@Father Name", SqlDbType.VarChar, 50);
            pram[2] = new SqlParameter("@Email", SqlDbType.VarChar, 50);
            pram[3] = new SqlParameter("@Land Line No", SqlDbType.Char, 10);
            pram[4] = new SqlParameter("@Mobile No", SqlDbType.VarChar, 50);
            pram[5] = new SqlParameter("@Country", SqlDbType.varchar, 10);
            pram[6] = new SqlParameter("@Gender", SqlDbType.VarChar, 20);
            //Now we set-uped all fiels in database in above lines
            //Now we will set-up form fields
            pram[0].Value = Name;
            pram[1].Value = FatherName;
            pram[2].Value = Email;
            pram[3].Value = LandLineNo;
            pram[4].Value = MobileNo;
            pram[5].Value = Country;
            pram[6].Value = Gender;
            //Now create loop to insert
            for (int i = 0; i < pram.Length; i++)
            cmd.CommandType = CommandType.Text;
        catch (System.Data.SqlClient.SqlException ex_msg)
            //Here will be catch elements
            string msg = "Error occured while inserting";
            msg += ex_msg.Message;
            throw new Exception(msg);
            //Here will be fially elements
    protected void Page_Load(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)
        //Here is the command inside the click event of button
        if (Name.Text == "")
            Response.Write("Please complete the form.");
            execution(Name.Text, FatherName.Text, Email.Text, LandLineNo.Text, MobileNo.Text, Country.Text, Gender.Text);
            conform.Visible = true;
            Name.Text = "";
            FatherName.Text = "";
              Email.Text = "";
            LandLineNo.Text = "";
            MobileNo.Text = "";
            Country.Text = "";
            Gender.Text = "";
            //address.Text = "";


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100