这是我的代码,但它给了我错误,我将在下面写下
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());
答案 0 :(得分:0)
您必须将您的命令对象全局声明为:
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());
}
}
}