我编制了一个录入界面,输入数据后提交成功
我的SQL表中除关键字段外,是允许为空的
但我录入空白数据时,确提交不成功。
我的代码是:(sbzl是一个DropDownList控件的名称)
<script language="C#" runat="server">
SqlConnection Conn;
protected void Page_Load(Object Src, EventArgs E )
{
Conn=new SqlConnection("server=localhost;database=jsjsb;uid=sa;pwd='sa'");
if (!IsPostBack) BindGrid();
}// 处理添加事件
public void submit_Click(Object sender, EventArgs e)
{
// 构造SQL语句
String strSQL="insert into jbxx(bh,sbzl,zypz) values('"+bh.Text+"', '"+sbzl.SelectedItem.Value+"', '"+zypz.Text+"')";
// 创建Command对象
SqlCommand Comm=new SqlCommand(strSQL,Conn);
// 打开连接
Comm.Connection.Open();
try
{
Comm.ExecuteNonQuery();
Label1.Text="数据添加成功";
Label2.Text="";
}
catch (SqlException)
{
Label2.Text="数据添加失败";
Label1.Text="";
}
// 关闭连接
Comm.Connection.Close();
BindGrid();
}// BindGrid()执行数据绑定
public void BindGrid()
{
// 创建DataAdapter对象
SqlDataAdapter da=new SqlDataAdapter("select * from jbxx",Conn);
// 创建并填充DataSet
DataSet ds = new DataSet();
da.Fill(ds);
}
void reset_Click(Object sender,EventArgs e)
{
//当单击取消按钮之后执行下面代码
bh.Text="";
//DropDownList控件该怎样清空?
zypz.Text="";
}
</script>
我的SQL表中除关键字段外,是允许为空的
但我录入空白数据时,确提交不成功。
我的代码是:(sbzl是一个DropDownList控件的名称)
<script language="C#" runat="server">
SqlConnection Conn;
protected void Page_Load(Object Src, EventArgs E )
{
Conn=new SqlConnection("server=localhost;database=jsjsb;uid=sa;pwd='sa'");
if (!IsPostBack) BindGrid();
}// 处理添加事件
public void submit_Click(Object sender, EventArgs e)
{
// 构造SQL语句
String strSQL="insert into jbxx(bh,sbzl,zypz) values('"+bh.Text+"', '"+sbzl.SelectedItem.Value+"', '"+zypz.Text+"')";
// 创建Command对象
SqlCommand Comm=new SqlCommand(strSQL,Conn);
// 打开连接
Comm.Connection.Open();
try
{
Comm.ExecuteNonQuery();
Label1.Text="数据添加成功";
Label2.Text="";
}
catch (SqlException)
{
Label2.Text="数据添加失败";
Label1.Text="";
}
// 关闭连接
Comm.Connection.Close();
BindGrid();
}// BindGrid()执行数据绑定
public void BindGrid()
{
// 创建DataAdapter对象
SqlDataAdapter da=new SqlDataAdapter("select * from jbxx",Conn);
// 创建并填充DataSet
DataSet ds = new DataSet();
da.Fill(ds);
}
void reset_Click(Object sender,EventArgs e)
{
//当单击取消按钮之后执行下面代码
bh.Text="";
//DropDownList控件该怎样清空?
zypz.Text="";
}
</script>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货