向DataGrid、DropDownList控件添加数据
2015-09-02 11:59
567 查看
string conStr=ConfigurationManager.AppSettings["ConStr"];
SqlConnection conn=new SqlConnection(conStr);
protected void Page_Load(object sender, EventArgs e)
{
if(! IsPostBack){
BindCategory();
BindDropNewsCategory();
}
else
{
return ;
}
}
void BindCategory()
{
string sql="select * from NewsCategoryInfo";
SqlDataAdapter da=new SqlDataAdapter(sql,conn);
DataSet ds=new DataSet();
da.Fill(ds);
if(ds.Table[0].Rows.Count>0)
{
GridView1.DataSource=ds.Table[0];
GridView1.DataBind();
}
void BindDropNewsCategory()
{
string sql="select * from NewsCategoryInfo";
SqlDataAdapter da=new SqlDataAdapter(sql,conn);
DataSet ds=new DataSet();
da.Fill(ds);
DropDownList1.DataTextField="NewsCategoryName";
DropDownList1.DataValueField="NewsCategoryID";
DropDownList1.DataSource=ds.Table[0];
GridView1.DataBind();
}
}
protection void Button_Click(object sender ,EventArgs e)
{
string sql="insert into NewsCategoryInfo (NewsCategoryName)value('+"txtCategoryName"+')";
SqlCommand cmd=new SqlCommand(sql ,conn); // 建立执行命令与数据库的关联
conn.Open(); // 打开数据库
int i=cmd.ExecuteNonQuery(); //执行命令
conn.Close();// 关闭数据库
if (i>0)
{
Response.Write("添加成功“);
BindCategory();
}
}
protection void Button1_Click(object sender ,EventArgs e)
{
int OkNewsCategoryID =Convert.ToInt32(DropDownList1.SelectedValue);
Respons.Write("添加成功”);
}
SqlConnection conn=new SqlConnection(conStr);
protected void Page_Load(object sender, EventArgs e)
{
if(! IsPostBack){
BindCategory();
BindDropNewsCategory();
}
else
{
return ;
}
}
void BindCategory()
{
string sql="select * from NewsCategoryInfo";
SqlDataAdapter da=new SqlDataAdapter(sql,conn);
DataSet ds=new DataSet();
da.Fill(ds);
if(ds.Table[0].Rows.Count>0)
{
GridView1.DataSource=ds.Table[0];
GridView1.DataBind();
}
void BindDropNewsCategory()
{
string sql="select * from NewsCategoryInfo";
SqlDataAdapter da=new SqlDataAdapter(sql,conn);
DataSet ds=new DataSet();
da.Fill(ds);
DropDownList1.DataTextField="NewsCategoryName";
DropDownList1.DataValueField="NewsCategoryID";
DropDownList1.DataSource=ds.Table[0];
GridView1.DataBind();
}
}
protection void Button_Click(object sender ,EventArgs e)
{
string sql="insert into NewsCategoryInfo (NewsCategoryName)value('+"txtCategoryName"+')";
SqlCommand cmd=new SqlCommand(sql ,conn); // 建立执行命令与数据库的关联
conn.Open(); // 打开数据库
int i=cmd.ExecuteNonQuery(); //执行命令
conn.Close();// 关闭数据库
if (i>0)
{
Response.Write("添加成功“);
BindCategory();
}
}
protection void Button1_Click(object sender ,EventArgs e)
{
int OkNewsCategoryID =Convert.ToInt32(DropDownList1.SelectedValue);
Respons.Write("添加成功”);
}
相关文章推荐
- openwrt手动恢复出厂设置
- 工业4.0在工业企业内实施的架构思考【一】
- apache-flume-1.5.0-bin windows
- linux命令:mkdir 命令详解
- Cloudera Hadoop 集群搭建(二)
- nginx使用笔记
- linux下jetty,tomcat中文乱码问题解决
- centos 一键安装lamp
- 架构对应的类型
- 操作系统性能监控-CPU使用率
- Ubuntu Linux 环境变量
- [Search Engine] 搜索引擎分类和基础架构概述
- BZOJ 题目1798: [Ahoi2009]Seq 维护序列seq(双标记线段树)
- 2.1.1、Dockerfile最佳实战
- docker toolbox在windows下如何为docker命令配置环境变量
- CentOS6 下安装JDK7
- docker+tomcat+java配置(ubuntu)
- nginx 多个80端口转发
- linux中export和source的作用和区别
- ubuntu 阿里云 常出问题 运维工作日志