【问题标题】:please help insert data into database请帮助将数据插入数据库
【发布时间】:2014-01-14 05:21:25
【问题描述】:

这是我的代码,但它给了我错误,我会写在下面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using lanox;

public partial class Info : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDataSource1"].ConnectionString);
            Connection.Open();
            string checkuser = "select count(*) from products where UserName='"+txtName.Text+"'";
           // SqlCommand Command = new SqlCommand(checkuser,Connection);

            int temp = Convert.ToInt32(Command.ExecuteScalar().ToString());

            if (temp == 1)
            {
                Response.Write("User Eneter invalid value ");
            }

            Connection.Close(); 
        }
    }

    protected void btnLast_Click(object sender, EventArgs e)
    {
        try
        {
            if (IsPostBack)
            {
               SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDataSource1"].ConnectionString);
                Connection.Open();
                string insert = "insert into products (ID,FirstName,LastName) values (@User,@F,@L)";
                SqlCommand Command = new SqlCommand(insert, Connection);
                Command.Parameters.AddWithValue("@User", txtID);
                Command.Parameters.AddWithValue("@F", txtName);
                Command.Parameters.AddWithValue("@L", txtLast);

                Command.ExecuteNonQuery();

                Response.Redirect("Info.aspx");
                Response.Write("Connection Sucssesfull");

                Connection.Close();
            }
        }
        catch (Exception ex)
        {
            Response.Write("Error" + ex.ToString());
        }
    }
}

我得到错误:

“/”应用程序中的服务器错误。

编译错误

描述:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。

编译器错误消息:CS0103:当前上下文中不存在名称“Command”

来源错误:
第 23 行:int temp = Convert.ToInt32(Command.ExecuteScalar().ToString());

【问题讨论】:

  • 如果您希望人们花时间帮助回答您的问题,那么您至少可以花时间正确格式化您的问题和代码。正是出于这个原因,问题预览才存在。使用它。
  • 那么 - 我们可以了解错误的一些细节吗?
  • 先生,我是新来的,我不知道你的意思

标签: c# sql-server database-connection sqlconnection


【解决方案1】:

您必须将您的命令对象全局声明为:

 SqlCommand Command=null;

删除关于页面加载的评论:

SqlCommand Command = new SqlCommand(checkuser,Connection);

这会解决你的问题。

整个代码将是:

public partial class Info : System.Web.UI.Page
{
    SqlCommand Command=null;
    protected void Page_Load(object sender, EventArgs e)
    {


        if (IsPostBack)
        {
      SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDataSource1"].ConnectionString);
            Connection.Open();
            string checkuser = "select count(*) from products where UserName='"+txtName.Text+"'";

             //If you dont want to declare globally , remove below comment

           // SqlCommand Command = new SqlCommand(checkuser,Connection);

            int temp = Convert.ToInt32(Command.ExecuteScalar().ToString());
            if (temp == 1)
            {
                Response.Write("User Eneter invalid value ");

            }


            Connection.Close(); 


        }
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        try
        {

            if (IsPostBack)
            {
               SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDataSource1"].ConnectionString);
                Connection.Open();
                string insert = "insert into products (ID,FirstName,LastName) values (@User,@F,@L)";
                Command = new SqlCommand(insert, Connection);
                Command.Parameters.AddWithValue("@User", txtID);
                Command.Parameters.AddWithValue("@F", txtName);
                Command.Parameters.AddWithValue("@L", txtLast);
                Command.ExecuteNonQuery();
                Response.Redirect("Info.aspx");
                Response.Write("Connection Sucssesfull");

                Connection.Close();

            }



        }

        catch (Exception ex)
        {
            Response.Write("Error" + ex.ToString());
        }
    }

}

【讨论】:

  • 它给了我另一个错误,它说“对象引用未设置为对象的实例。” SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDataSource1"].ConnectionString);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
相关资源
最近更新 更多