【发布时间】:2021-12-01 08:11:04
【问题描述】:
我有代码读取数据库中的空行,如果数据库中没有行则 textbox = "0"
我的代码:
protected void CheckNota()
{
string vNota;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd1 = new SqlCommand("select ISNULL ((KdNota), 0) as vKdNota from tProdukBeliHead where KdNota = '" + txtKdBeli.Text.Trim() + "'", con))
//using (SqlCommand cmd1 = new SqlCommand("select KdNota from tProdukBeliHead where KdNota = '" + txtKdBeli.Text.Trim() + "'", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd1))
{
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows[0]["vKdNota"] == DBNull.Value)
{
vNota = "0";
}
else
{
vNota = dt.Rows[0]["KdNota"].ToString();
}
}
}
}
}
但是文本框没有显示值 0,只报告这个:位置 0 没有行。
谢谢
【问题讨论】:
-
您的代码假定有一行。如果您的查询未返回任何结果,则
dt.Rows[0]将不存在。您应该检查数据表是否确实有行。看看这个:stackoverflow.com/questions/6264554/… -
使用适当的参数化,不要在查询中注入数据
-
您没有在查询中选择
KdNota。 KdNota vKdNota -
谢谢 squillman,现在工作
标签: c# asp.net sql-server