在没有任何数据时进行无效的读取尝试。
using System;using System.Data;
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;
using System.Data.SqlClient;
public partial class huifuliuyancaozuo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string str = "server=.\\SQLEXPRESS;database=xinwen;Trusted_Connection=true";
SqlConnection con = new SqlConnection(str);
con.Open();
string str1 = "select liuyanren,liuyanzhuti from liuyan where liuyanren='" + Request.QueryString["liuyanren"] + "'";
SqlCommand com = new SqlCommand(str1, con);
SqlDataReader reader = com.ExecuteReader();//定义数据阅读器来读取记录
reader.Read();//使其读取第一条记录
//设定文本框控件显示读取到的信息
TextBox1.Text = reader.GetString(0);
TextBox2.Text = reader.GetString(1);
reader.Close();
con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = "server=.\\SQLEXPRESS;database=xinwen;Trusted_Connection=true";
SqlConnection con = new SqlConnection(str);
con.Open();
string str1 = "select * from huifuliuyan values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox3.Text + "','" + txtDate.Text + "')";
SqlCommand com = new SqlCommand(str1, con);
if (com.ExecuteNonQuery() > 0)//判断受影响的行数是否大于0,如果大于0,表示数据成功插入到表中,
Response.Write("<script>alert('回复成功!');window.location='GLY.aspx';</script>");
else
Response.Write("<script>alert('回复失败!');</script>");
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox4_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox3_TextChanged1(object sender, EventArgs e)
{
}
protected void txtDate_TextChanged(object sender, EventArgs e)
{
}
}