NewsService newsService = new NewsService();
protected void Page_Load(object sender, EventArgs e)
{
ddlNewsType.DataSource = newsService.Get_D_NewsType();
ddlNewsType.DataValueField = "ClsID";
ddlNewsType.DataTextField = "ClsName";
ddlNewsType.DataBind();
ddlNewsType.Items.Insert(0, " --请选择分类--");
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string newsTitle = txtNewsName.Text.Trim();
string newsType = ddlNewsType.SelectedItem.Text;
string newsFrom = txtNewsSource.Text.Trim();
int ClsID = Convert.ToInt32(ddlNewsType.SelectedValue);
string newsContent = CKEditorControl1.Text;
DateTime time = DateTime.Now;
if (newsService.Add_T_News(ClsID,newsTitle,newsContent,time,newsFrom))
{
Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", "<script>alert('添加成功')</script>");
}
else
{
Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", "<script>alert('添加失败')</script>");
}
}
上面是错误出现的地方 就这里int ClsID = Convert.ToInt32(ddlNewsType.SelectedValue);
下面是访问数据库
/// <summary>
/// 想新闻表中添加新闻
/// </summary>
/// <param name="ClsID">新闻类别ID</param>
/// <param name="InfoName">新闻名称</param>
/// <param name="InfoNote">新闻内容</param>
/// <param name="InfoDate">添加新闻的时间</param>
/// <param name="writefrom">新闻来源</param>
/// <returns></returns>
public bool Add_T_News(int ClsID, string InfoName, string InfoNote, DateTime InfoDate, string writefrom)
{
try
{
string sqlStr = "insert into T_News(ClsID,InfoName,InfoNote,InfoDate,writefrom) values(" + ClsID + ",'" + InfoName + "','" + InfoNote + "'," + InfoDate + ",'" + writefrom + "')";
OleDbCommand cmd = new OleDbCommand(sqlStr, con);
con.Open();
if (cmd.ExecuteNonQuery() > 0)
{
return true;
}
return false;
}
catch (Exception e)
{
throw e;
}
finally
{
con.Close();
}
}
连接的是ACCESS数据库
------解决方案--------------------------------------------------------