asp.net中 DropDownList1 读取出来的值,在插入到另外一个表中,怎么value的怎么总是一个
SqlDataAdapterda=newSqlDataAdapter();SqlConnectionconn=newSqlConnection();conn.Connec...
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "xxxxx";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from new_class";
cmd.Connection = conn;
da.SelectCommand = cmd;
DataSet date =new DataSet();
da.Fill(date,"news");
DropDownList1.DataSource = date.Tables["news"].DefaultView;
DropDownList1.DataTextField = "news_classa";
DropDownList1.DataValueField = "news_class1";
DropDownList1.DataBind();
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "xxxxxx";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into news(new_title,new_title_t,new_titlev,new_content,new_alrt,new_datime) values (@new_title,@new_title_t,@new_titlev,@new_content,@new_alrt,@new_datime)";
cmd.Connection = conn;
SqlParameter new_title_t = new SqlParameter("@new_title_t", SqlDbType.NVarChar, 1000);
SqlParameter new_titlev = new SqlParameter("@new_titlev", SqlDbType.Int, 1000);
new_title_t.Value = DropDownList1.SelectedItem.Text;
new_titlev.Value = DropDownList1.SelectedItem.Value;
cmd.Parameters.Add(new_title_t);
cmd.Parameters.Add(new_titlev);
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
}
}
请大神帮忙看一下为何 展开
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "xxxxx";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from new_class";
cmd.Connection = conn;
da.SelectCommand = cmd;
DataSet date =new DataSet();
da.Fill(date,"news");
DropDownList1.DataSource = date.Tables["news"].DefaultView;
DropDownList1.DataTextField = "news_classa";
DropDownList1.DataValueField = "news_class1";
DropDownList1.DataBind();
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "xxxxxx";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into news(new_title,new_title_t,new_titlev,new_content,new_alrt,new_datime) values (@new_title,@new_title_t,@new_titlev,@new_content,@new_alrt,@new_datime)";
cmd.Connection = conn;
SqlParameter new_title_t = new SqlParameter("@new_title_t", SqlDbType.NVarChar, 1000);
SqlParameter new_titlev = new SqlParameter("@new_titlev", SqlDbType.Int, 1000);
new_title_t.Value = DropDownList1.SelectedItem.Text;
new_titlev.Value = DropDownList1.SelectedItem.Value;
cmd.Parameters.Add(new_title_t);
cmd.Parameters.Add(new_titlev);
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
}
}
请大神帮忙看一下为何 展开
1个回答
展开全部
DropDownList1.DataSource = date.Tables["news"].DefaultView;
//断点调试一下这段代码,date.Tables["news"].DefaultView,获取的数据源是不是你想要的结果。
//再看一下列名,是否是news_classa与news_class1
追问
预览页面,新闻分类也是能出来的,但我在btn 提交的时候,选择新闻类别的时候,出入的值总是一个,
追答
Page_Load()
{
if(!Page.IsPostBack)
{
DropDownList1.DataSource = date.Tables["news"].DefaultView;
DropDownList1.DataTextField = "news_classa";
DropDownList1.DataValueField = "news_class1";
DropDownList1.DataBind();
}
}
是这样?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询