未将对象引用设置到对象的实例。
using System;using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class Inte易做图ce_Guest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string _Company = cut(Request["_Company"]);
string _Picture = cut(Request["Picture"]);
string _Categories = cut(Request["Categories"]);
string sql = "select count(*) from Guest where Company='" + _Company + "'and Picture='" + _Picture + "'and Categories='"+_Categories+"'";
string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand cmd = new SqlCommand(sql, objConnection);
cmd.ExecuteNonQuery();
cmd.Dispose();
objConnection.Dispose();
objConnection.Close();
Response.Write("成功");
}
catch(Exception ex)
{
Response.Write("失败"+ex.Message);
}
}
private string cut(string content)
{
return content.Trim().Replace("'", "").Replace("%", "").Replace("*","").Replace("insert", "").Replace("into", "").Replace("update", "").Replace("delete", "").Replace("select", "");
}
}
这段代码为什么会报错,我找了很久不知道原因,请高手指点下谢谢。 --------------------编程问答-------------------- objConnection.Dispose();
objConnection.Close();
这两句任选其一,你先Dispose,再Close肯定出错 --------------------编程问答-------------------- 你是说要这两句其中的一句对吗?
objConnection.Dispose();
objConnection.Close();
--------------------编程问答-------------------- 我试过了··
但是他还是报“未将对象引用设置到对象的实例”
请您说仔细点可以吗?
这个接口是给Flash用的,以前我没有写过 --------------------编程问答-------------------- 检查下连接字符串吧 --------------------编程问答-------------------- 你传过来的地址没有你所要获取的字符串.一定是这个. --------------------编程问答-------------------- cmd.Dispose(); 这个不要可以吗 --------------------编程问答--------------------
string _Company = cut(Request["_Company"]);
string _Picture = cut(Request["Picture"]);
string _Categories = cut(Request["Categories"]);
string sql = string.Format("select count(*) from Guest where Company='{0}' and Picture='{1}'and Categories='{2}'", _Company, _Picture, _Categories);
string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
using (SqlConnection objConnection = new SqlConnection(strConnection))
{
objConnection.Open();
SqlCommand cmd = new SqlCommand(sql, objConnection);
cmd.ExecuteNonQuery();
}
Response.Write("成功");
最好将错误信息一个给出来 --------------------编程问答-------------------- 这样看看:
--------------------编程问答-------------------- private string cut(string content)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class Inte易做图ce_Guest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string _Company = cut(Request["_Company"]);
string _Picture = cut(Request["Picture"]);
string _Categories = cut(Request["Categories"]);
string sql = "select count(*) from Guest where Company='" + _Company + "'and Picture='" + _Picture + "'and Categories='" + _Categories + "'";
string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand cmd = new SqlCommand(sql, objConnection);
cmd.ExecuteNonQuery();
cmd.Dispose();
objConnection.Dispose();
Response.Write("成功");
}
catch (Exception ex)
{
Response.Write("失败" + ex.Message);
}
}
private string cut(string content)
{
if (string.IsNullOrEmpty(content))
return string.Empty;
return content.Trim().Replace("'", "").Replace("%", "").Replace("*", "").Replace("insert", "").Replace("into", "").Replace("update", "").Replace("delete", "").Replace("select", "");
}
}
是你这个方法有问题,字符串为空的时候,你要判断下。 --------------------编程问答-------------------- string _Company;
try
{
Company = cut(Request["_Company"]);
}catch{
Company = "";
}
.....
以下几个一样. --------------------编程问答-------------------- 失败对象名 'Guest' 无效。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class Inte易做图ce_Guest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string _Company = cut(Request["_Company"]);
string _Picture = cut(Request["Picture"]);
string _Categories = cut(Request["Categories"]);
string sql = "select count(*) from Guest where Company='" + _Company + "'and Picture='" + _Picture + "'and Categories='" + _Categories + "'";
string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand cmd = new SqlCommand(sql, objConnection);
cmd.ExecuteNonQuery();
cmd.Dispose();
objConnection.Dispose();
Response.Write("成功");
}
catch (Exception ex)
{
Response.Write("失败" + ex.Message);
}
}
private string cut(string content)
{
if (string.IsNullOrEmpty(content))
return string.Empty;
return content.Trim().Replace("'", "").Replace("%", "").Replace("*", "").Replace("insert", "").Replace("into", "").Replace("update", "").Replace("delete", "").Replace("select", "");
}
}
--------------------编程问答-------------------- 问题解决了吗?
你是不是直接复制的页面啊? --------------------编程问答--------------------
连接字符串有没有问题?数据库中有没有Guest这个表? --------------------编程问答-------------------- 没有!
--------------------编程问答-------------------- 谢谢··可以啦··
感谢大家了··
可以加你们为好友吗··
谢谢
补充:.NET技术 , ASP.NET