前页:<a href="view.aspx?n_id=<%# DataBinder.Eval(Container.DataItem,"n_id")%>" target="_blank"><%# DataBinder.Eval(Container.DataItem,"n_title")%></a>后页
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 view : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String strconn = "server=(local)\\sqlexpress;database=...;Uid=...;pwd=...;";
SqlConnection strcon = new SqlConnection(strconn);
strcon.Open();
if (!Page.IsPostBack)
{
String n_id = Request["n_id"];
Response.Write (n_id);
if (n_id == null)
{
string str = "<script language=javascript>alert('记录不存在!');window.close();</script>";
Response.Write(str);
}
String show = "select * from news where n_id='" + n_id + "'";
SqlCommand com = new SqlCommand(show, strcon);
SqlDataReader cmd = com.ExecuteReader();
cmd.Read();
Label1.Text = cmd["n_title"].ToString();
Label2.Text = cmd["n_content"].ToString(); }
}
}我要问的是:当我在浏览器中以
http://localhost/view.aspx?n_id=7
浏览时没有问题.但将n_id的值删掉,如:
http://localhost/view.aspx?n_id=
时,
if (n_id == null)
{
string str = "<script language=javascript>alert('记录不存在!');window.close();</script>";
Response.Write(str);
}无法判断其值为空.还继续往后执行.为什么?有什么办法解决,没有ID值时,让判断语句后的javascript语句执行?急等.......请帮忙,谢谢!
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 view : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String strconn = "server=(local)\\sqlexpress;database=...;Uid=...;pwd=...;";
SqlConnection strcon = new SqlConnection(strconn);
strcon.Open();
if (!Page.IsPostBack)
{
String n_id = Request["n_id"];
Response.Write (n_id);
if (n_id == null)
{
string str = "<script language=javascript>alert('记录不存在!');window.close();</script>";
Response.Write(str);
}
String show = "select * from news where n_id='" + n_id + "'";
SqlCommand com = new SqlCommand(show, strcon);
SqlDataReader cmd = com.ExecuteReader();
cmd.Read();
Label1.Text = cmd["n_title"].ToString();
Label2.Text = cmd["n_content"].ToString(); }
}
}我要问的是:当我在浏览器中以
http://localhost/view.aspx?n_id=7
浏览时没有问题.但将n_id的值删掉,如:
http://localhost/view.aspx?n_id=
时,
if (n_id == null)
{
string str = "<script language=javascript>alert('记录不存在!');window.close();</script>";
Response.Write(str);
}无法判断其值为空.还继续往后执行.为什么?有什么办法解决,没有ID值时,让判断语句后的javascript语句执行?急等.......请帮忙,谢谢!
if (string.IsNullOrEmpty(.n_id))
{
string str = " <script language=javascript>alert('记录不存在!');window.close(); </script>";
Response.Write(str);
}
如果是http://localhost/view.aspx, 即没有n_id参数,返回的则是null
还是不行啊.http://localhost/view.aspx?n_id=7
浏览正常.
但.手动的将地址栏
http://localhost/view.aspx?n_id=7
中的7删除后回车,代码依然继续往下执行.http://localhost/view.aspx?n_id=
异常详细信息: System.InvalidOperationException: 在没有任何数据时进行无效的读取尝试。源错误:
行 31: SqlDataReader cmd = com.ExecuteReader();
行 32: cmd.Read();
行 33: Label1.Text = cmd["n_title"].ToString(); //没有任何数据时进行无效的读取尝试
行 34: Label2.Text = cmd["n_content"].ToString();
if (string.IsNullOrEmpty( Request["n_id"]))
{
string str = " <script language=javascript>alert('记录不存在!');window.close(); </script>";
Response.Write(str);
}
//去数据时要加个判断,是否读到了数据
SqlDataReader cmd = com.ExecuteReader();
if( cmd.Read())
{
Label1.Text = cmd["n_title"].ToString(); //没有任何数据时进行无效的读取尝试
Label2.Text = cmd["n_content"].ToString();
}
null是不分配内存的
if( cmd.Read())
{
try{
Label1.Text = cmd["n_title"].ToString(); //这儿加个异常处理
}
catch{//你 的操作}
Label2.Text = cmd["n_content"].ToString();
}
{
if (Request["n_id"]== null && Request["n_id"]!="")
{
string str = " <script language=javascript>alert('记录不存在!');window.close(); </script>";
Response.Write(str);
}
string n_id = Request["n_id"];
string show = "select * from news where n_id='" + n_id + "'";
SqlCommand com = new SqlCommand(show, strcon);
SqlDataReader cmd = com.ExecuteReader();
cmd.Read();
Label1.Text = cmd["n_title"].ToString();
Label2.Text = cmd["n_content"].ToString(); }
try
{
Label1.Text = cmd["n_title"].ToString();
Label2.Text = cmd["n_content"].ToString();
}
catch
{
string str = " <script language=javascript>alert('记录不存在!');window.close(); </script>";
Response.Write(str); }方法二:把ID改成int
try
{
int n_ID = Convert.ToInt32(Request.QueryString["ID"]);
}
catch
{
string str = " <script language=javascript>alert('记录不存在!');window.close(); </script>";
Response.Write(str);
} String show = "select * from news where n_id='" + n_id.ToString() + "'";