大家帮我看一下留言板的一部分代码
public partial class Ex09_05 : System.Web.UI.Page
{
protected SqlConnection myConnection = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count == 0)
{
Response.Redirect( "Ex09_01.aspx ");
}
else
{
if (!IsPostBack)
{
postID = Request[ "PostID "];
}
myConnection.ConnectionString = "server=Localhost;uid=sa;pwd=sa;database=Aspnet ";
BindData();
}
}
private void BindData()
{
string selCmd1 = "select * from Ex09_GuestBook where PostID= " + postID;
SqlDataAdapter da1 = new SqlDataAdapter(selCmd1, myConnection);
string selCmd2 = "select * from Ex09_GuestBook where ParentID= " + postID;
SqlDataAdapter da2 = new SqlDataAdapter(selCmd2, myConnection);
DataSet ds = new DataSet();
da1.Fill(ds, "host ");
da2.Fill(ds, "guest ");
Label1.Text = ds.Tables[ "host "].Rows[0][4].ToString();
int reCount = ds.Tables[ "guest "].Rows.Count;
Label2.Text = reCount.ToString();
Label3.Text = ds.Tables[ "host "].Rows[0][2].ToString();
Label4.Text = ds.Tables[ "host "].Rows[0][4].ToString();
HyperLink1.Text = ds.Tables[ "host "].Rows[0][3].ToString();
HyperLink1.NavigateUrl = "Ex09_06.aspx?UserName= " + HyperLink1.Text;
DataList1.DataSource = ds;
DataList1.DataMember = "guest ";
DataList1.DataBind();
this.DataBind();
myConnection.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string Name = Session[ "username "].ToString();
string insCmd = "insert Ex09_GuestBook values( " + postID + ",getdate(), ' " + Name + " ', ' ', ' " + TextBox2.Text + " ') ";
SqlCommand myCmd = new SqlCommand(insCmd, myConnection);
myCmd.Connection.Open();
myCmd.ExecuteNonQuery();
myCmd.Connection.Close();
myConnection.Close();
BindData();
TextBox2.Text = " ";
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = " ";
}
}
老是说找不到postID。我qq370166067,方便的话在qq上说。
[解决办法]
public partial class Ex09_05 : System.Web.UI.Page
{
postID = Request[ "PostID "];
...
}
放在外面.
[解决办法]
string selCmd1 = "select * from Ex09_GuestBook where PostID= ' " + postID+ " ' ";
string selCmd2 = "select * from Ex09_GuestBook where ParentID= ' " + postID+ " ' ";
照上面这样改试试看..