Click here to Skip to main content
15,065,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 = "";
Updated 25-Jun-11 21:47pm

If you do have a table with column which has a space, you have to work with a square brace in order to access it.So when you want to access Father name column, work with [Father Name] and so and so forth.

So your insert sql statement should look like this

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)"
You have provided too little information. Posting some code might help here.
But by the look of things, it appears as if the connection to the database is either not open, or the table you are trying to insert into does not exist (or the table structure is completely different).
asaboor12 26-Jun-11 3:34am
ok i hae posted the code as well
why not use store procedure?

for example

in db

Create procedure sp_Insert
   @Name varchar(50), @FatherName varchar(50), @Email varchar(20), @LandLineNo varchar(15), @MobileNo varchar(15), 
   @Country varchar(50), @Gender varchar(6)
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)

call store procedure in UI like button event
     sqlConnection = new SqlConnection(dbConnectionString);
     SqlCommand command = new SqlCommand("sp_Insert", sqlConnection);
     command.CommandType = CommandType.StoredProcedure;
     command.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name.Text;
     command.Parameters.Add("@Father Name", SqlDbType.VarChar).Value = FatherName.Text;
     command.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email.Text;
     command.Parameters.Add("@Land Line No", SqlDbType.VarChar).Value = LandLineNo.Text;
     command.Parameters.Add("@Mobile No", SqlDbType.VarChar).Value = Mobile.Text;
     command.Parameters.Add("@Country", SqlDbType.VarChar).Value = Country.Text;
     command.Parameters.Add("@Gender", SqlDbType.VarChar).Value = Gender.Text;
     return command.ExecuteNonQuery();
catch (SqlException ex)
     Console.WriteLine("SQL Error" + ex.Message.ToString());
     return 0;

try this
asaboor12 26-Jun-11 5:36am
it is giving error while saving stored procedure "incorrect syntax near Name"
mahabubur rahman 27-Jun-11 1:09am
check your connection string if its ok next check your database table attributes that all attributes are varchar if any different attributes just chage it for example
command.Parameters.Add("@Mobile No", SqlDbType.Int).Value = Convert.ToInt32(Mobile.Text);
I improve my code try it again hope it will be work. best of luck

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