连接到数据库时,asp.net注册页面中出现错误 [英] error in asp.net registartion page while connecting to database

查看:60
本文介绍了连接到数据库时,asp.net注册页面中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

em插入数据库时​​出现以下错误

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 = "";
        }

推荐答案

如果您确实有一个带有带空格的列的表,则必须使用方括号才能访问它.想要访问Father name列,使用[Father Name]等,以此类推.

因此,您的插入sql语句应如下所示

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).


为什么不使用存储过程?

例如

在db

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)




UI中的调用存储过程,例如按钮事件
-------




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


这篇关于连接到数据库时,asp.net注册页面中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆