我写了一个备注页面 打开备注页面要显示数据库的信息 可是数据库里没有数据就会报错 我应该怎么写判断语句
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.Common;public partial class khgl_beizhu : System.Web.UI.Page
{
string c_id = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
c_id = Request["c_id"] + "";
binduser(c_id);
}
} private void binduser(string id)
{
string Sql = "select * from t_beizhu where c_id='" + c_id + "'";
DataTable dt =conn.SqlQuery(Sql);
gszz.Text = dt.Rows[0]["c_gszz"].ToString();
gsjj.Text = dt.Rows[0]["c_gsjj"].ToString();
jtlx.Text = dt.Rows[0]["c_jtlx"].ToString();
khxq.Text = dt.Rows[0]["c_khxq"].ToString();
jzzk.Text = dt.Rows[0]["c_jzzk"].ToString();
xxh.Text = dt.Rows[0]["c_xxh"].ToString();
qt.Text = dt.Rows[0]["c_qt"].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
int id = Convert.ToInt16(Request.QueryString["id"]);
string a = gszz.Text.ToString();
string b = gsjj.Text.ToString();
string c = jtlx.Text.ToString();
string d = khxq.Text.ToString();
string q = jzzk.Text.ToString();
string f = xxh.Text.ToString();
string g = qt.Text.ToString();
if (Request["c_id"]+""!="")
{
string Sql = "Update t_beizhu set [c_gszz]='" + a + "',[c_gsjj]='"+ b +"',[c_jtlx]='"+c+"',[c_khxq]='"+d+"',[c_jzzk]='"+q+"',[c_xxh]='"+f+"',[c_qt]='"+g+"' where c_id=" + c_id + "";
if (conn.ExecuteCommand(Sql)!= 0)
{
Response.Write("<script>alert('保存成功')</script>");
Response.Write("<script>window.location='detail.aspx'</script>");
} else
{
conn.Message(this, "保存失败!");
}
}
else
{
string sql = "insert into t_beizhu (c_gszz,c_gsjj,c_jtlx,c_khxq,c_jzzk,c_xxh,c_qt) values ('" + gszz.Text + "','" + gsjj.Text + "','" + jtlx.Text + "','" + khxq.Text + "','" + jzzk.Text + "','" + xxh.Text + "','" + qt.Text + "')"; if (conn.ExecuteCommand(sql) != 0)
{
Response.Write("<script>alert('添加成功')</script>");
Response.Write("<script>window.location='detail.aspx'</script>");
} }
}
}
这是我的代码 求大神 跪求
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.Common;public partial class khgl_beizhu : System.Web.UI.Page
{
string c_id = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
c_id = Request["c_id"] + "";
binduser(c_id);
}
} private void binduser(string id)
{
string Sql = "select * from t_beizhu where c_id='" + c_id + "'";
DataTable dt =conn.SqlQuery(Sql);
gszz.Text = dt.Rows[0]["c_gszz"].ToString();
gsjj.Text = dt.Rows[0]["c_gsjj"].ToString();
jtlx.Text = dt.Rows[0]["c_jtlx"].ToString();
khxq.Text = dt.Rows[0]["c_khxq"].ToString();
jzzk.Text = dt.Rows[0]["c_jzzk"].ToString();
xxh.Text = dt.Rows[0]["c_xxh"].ToString();
qt.Text = dt.Rows[0]["c_qt"].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
int id = Convert.ToInt16(Request.QueryString["id"]);
string a = gszz.Text.ToString();
string b = gsjj.Text.ToString();
string c = jtlx.Text.ToString();
string d = khxq.Text.ToString();
string q = jzzk.Text.ToString();
string f = xxh.Text.ToString();
string g = qt.Text.ToString();
if (Request["c_id"]+""!="")
{
string Sql = "Update t_beizhu set [c_gszz]='" + a + "',[c_gsjj]='"+ b +"',[c_jtlx]='"+c+"',[c_khxq]='"+d+"',[c_jzzk]='"+q+"',[c_xxh]='"+f+"',[c_qt]='"+g+"' where c_id=" + c_id + "";
if (conn.ExecuteCommand(Sql)!= 0)
{
Response.Write("<script>alert('保存成功')</script>");
Response.Write("<script>window.location='detail.aspx'</script>");
} else
{
conn.Message(this, "保存失败!");
}
}
else
{
string sql = "insert into t_beizhu (c_gszz,c_gsjj,c_jtlx,c_khxq,c_jzzk,c_xxh,c_qt) values ('" + gszz.Text + "','" + gsjj.Text + "','" + jtlx.Text + "','" + khxq.Text + "','" + jzzk.Text + "','" + xxh.Text + "','" + qt.Text + "')"; if (conn.ExecuteCommand(sql) != 0)
{
Response.Write("<script>alert('添加成功')</script>");
Response.Write("<script>window.location='detail.aspx'</script>");
} }
}
}
这是我的代码 求大神 跪求
解决方案 »
- 请高手们帮我看下这段代码!加粗这句代码系统提示未将对象引用设置到对象的实例。
- private void Form1_MouseDown(object sender, MouseEventArgs e)
- 如何取消vs2005检查html代码的错误呀。。。我写的不规范他检查出96个错误96个警告严重影响及其的 运行速度
- 如何从一个aspx网站中post数据
- 获取Cookie的代码,各位帮我看看哪里出错了?
- 如何在EXCEL中点击按钮插入日期?
- SNMP V3 依然不安全么?
- WPFToolkit Xaml 引用不起作用。
- Professional C#, 2nd Edition的下载?
- 在TreeView中单击了一个Node后,如何刷新旁边文本框的相应内容?
- 请问,ADO.NET中的行版本
- 新手求教高手指点这段代码该怎么写
if(dt!=null && dt.Rows.Count>0)
{}if(dt.Rows[0]["c_gszz"]!=DBNull.Value)
{}
?
private void binduser(string id)
{
string Sql = "select * from t_beizhu where c_id='" + c_id + "'";
DataTable dt =conn.SqlQuery(Sql);
if(dt!=null&&dt.Rows.Count>0)
{
gszz.Text = dt.Rows[0]["c_gszz"].ToString();
gsjj.Text = dt.Rows[0]["c_gsjj"].ToString();
jtlx.Text = dt.Rows[0]["c_jtlx"].ToString();
khxq.Text = dt.Rows[0]["c_khxq"].ToString();
jzzk.Text = dt.Rows[0]["c_jzzk"].ToString();
xxh.Text = dt.Rows[0]["c_xxh"].ToString();
qt.Text = dt.Rows[0]["c_qt"].ToString();
}
}
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.Common;public partial class khgl_beizhu : System.Web.UI.Page
{
string c_id = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
c_id = Request["c_id"] + "";
binduser(c_id);
}
} private void binduser(string id)
{
string Sql = "select * from t_beizhu where c_id='" + c_id + "'";
DataTable dt =conn.SqlQuery(Sql);
if (dt != null && dt.Rows.Count > 0)
{
gszz.Text = dt.Rows[0]["c_gszz"].ToString();
gsjj.Text = dt.Rows[0]["c_gsjj"].ToString();
jtlx.Text = dt.Rows[0]["c_jtlx"].ToString();
khxq.Text = dt.Rows[0]["c_khxq"].ToString();
jzzk.Text = dt.Rows[0]["c_jzzk"].ToString();
xxh.Text = dt.Rows[0]["c_xxh"].ToString();
qt.Text = dt.Rows[0]["c_qt"].ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int id = Convert.ToInt16(Request.QueryString["id"]);
string a = gszz.Text.ToString();
string b = gsjj.Text.ToString();
string c = jtlx.Text.ToString();
string d = khxq.Text.ToString();
string q = jzzk.Text.ToString();
string f = xxh.Text.ToString();
string g = qt.Text.ToString();
if(dt.Rows[0]["c_gszz"]!=DBNull.Value)
{
string Sql = "Update t_beizhu set [c_gszz]='" + a + "',[c_gsjj]='"+ b +"',[c_jtlx]='"+c+"',[c_khxq]='"+d+"',[c_jzzk]='"+q+"',[c_xxh]='"+f+"',[c_qt]='"+g+"' where c_id=" + c_id + "";
if (conn.ExecuteCommand(Sql)!= 0)
{
Response.Write("<script>alert('保存成功')</script>");
Response.Write("<script>window.location='detail.aspx'</script>");
} else
{
conn.Message(this, "保存失败!");
}
}
else
{
string sql = "insert into t_beizhu (c_gszz,c_gsjj,c_jtlx,c_khxq,c_jzzk,c_xxh,c_qt) values ('" + gszz.Text + "','" + gsjj.Text + "','" + jtlx.Text + "','" + khxq.Text + "','" + jzzk.Text + "','" + xxh.Text + "','" + qt.Text + "')"; if (conn.ExecuteCommand(sql) != 0)
{
Response.Write("<script>alert('添加成功')</script>");
Response.Write("<script>window.location='detail.aspx'</script>");
} }
}
}
这样对么?
{
int id = Convert.ToInt16(Request.QueryString["id"]);
string a = gszz.Text.ToString();
string b = gsjj.Text.ToString();
string c = jtlx.Text.ToString();
string d = khxq.Text.ToString();
string q = jzzk.Text.ToString();
string f = xxh.Text.ToString();
string g = qt.Text.ToString();
这里的条件好像也要改
if (Request["c_id"]+""!="")
{
string Sql = "Update t_beizhu set [c_gszz]='" + a + "',[c_gsjj]='"+ b +"',[c_jtlx]='"+c+"',[c_khxq]='"+d+"',[c_jzzk]='"+q+"',[c_xxh]='"+f+"',[c_qt]='"+g+"' where c_id=" + c_id + "";
if (conn.ExecuteCommand(Sql)!= 0)
{
Response.Write("<script>alert('保存成功')</script>");
Response.Write("<script>window.location='detail.aspx'</script>");
} else
{
conn.Message(this, "保存失败!");
}
}