Click here to Skip to main content
14,385,566 members
Rate this:
Please Sign up or sign in to vote.
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
            ["registrationConnectionString1"].ConnectionString;
        //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.
        try
        {
            conn.Open();
            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.Parameters.Add(pram[i]);
            }
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        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);
        }
        finally
        {
            //Here will be fially elements
            conn.Close();
        }
    }
    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.");
        }
        else
        {
            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 = "";
        }
Posted
Updated 25-Jun-11 22:47pm
v3
Rate this:
Please Sign up or sign in to vote.

Solution 4

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)"
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

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).
   
v2
Comments
asaboor12 26-Jun-11 3:34am
   
ok i hae posted the code as well
Rate this:
Please Sign up or sign in to vote.

Solution 3

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)
)
as
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
-------
try
  {
     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;
     sqlConnection.Open();
     return command.ExecuteNonQuery();
     sqlConnection.Close();
  }
catch (SqlException ex)
  {
     Console.WriteLine("SQL Error" + ex.Message.ToString());
     return 0;
  }



try this
   
v3
Comments
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100