我吧一条记录读取到dataset中,需要提取几个字段的值,请问怎么写法?
解决方案 »
- 服务器端向form提交数据
- showmodaldialog 窗口焦点问题
- 用radiobuttonlist绑定数据库里的数据?
- GridView与Substitution的结合使用
- Application_Start(object sender, EventArgs e)这个事件能持续多长时间,还是他就一个简单的动作
- 帮忙看看,下面的代码转成vb应该怎么转啊?麻烦了!~
- 用iframe嵌套的aspx页如何能得到嵌套这个aspx的页的url地址?
- 请教各位大虾有关onserverchange问题
- 如果有个像DNN的开源项目出现,你会参与吗?
- ASP.NET中文乱码的问题。
- 判断CheckBoxList必须选中一个
- 能者 进来
//Rows[0]行号
//["ID"]你的字段
Response.Write(dt.Rows[0]["ID"].ToString());
//Tables[0]你的Datatable索引,默认从0开始算,如果有指定DataTable名,也可以换成那个名字
//Rows[0]行号
//["ID"]字段
ds.Tables[0].Rows[0]["ID"]
{
if(!Page.IsPostBack)
_connstring = WebConfigurationManager.ConnectionStrings["proconnectionstring"].ConnectionString;
SqlConnection con = new SqlConnection(_connstring);
//得到配置连接
//得到参数
int proid = 1;
if (Request.QueryString["id"] != null)
{
proid = int.Parse(Request.QueryString["proid"].ToString());
}
//传递参数查询数据
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select *from pro where proid=@proid";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@proid", proid); /* SqlCommand cmd = new SqlCommand("select * from pro where proid="+proid,con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet dst = new DataSet();
sda.Fill(dst);*/ SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet dst = new DataSet();
using (con)
{
sda.Fill(dst);
}
FormView1.DataSource = dst;
FormView1.DataBind(); /*
SqlCommand cmand = new SqlCommand("select * from test where id=" + id, con);
cmand.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmand);
DataSet ds = new DataSet();
da.Fill(ds);
*/ // this.div11.InnerText = ds.Tables[0].Rows[0]["title"].ToString();
// this.divcontent.InnerText = ds.Tables[0].Rows[0]["content"].ToString();
Page.Header.Title = dst.Tables[0].Rows[0]["pagetitle"].ToString();
HtmlMeta metaDesc = new HtmlMeta();
metaDesc.Name = "DESCRIPTION";
metaDesc.Content = dst.Tables[0].Rows[0]["pagedescriptions"].ToString(); HtmlMeta metakeywords = new HtmlMeta();
metakeywords.Name = "KEYWORDS";
metakeywords.Content = dst.Tables[0].Rows[0]["pagekeywords"].ToString(); HtmlHead head = (HtmlHead)Page.Header;
head.Controls.Add(metaDesc);
head.Controls.Add(metakeywords);
}只是一条记录啊,根据记录的ID,怎么从page_load 事件中读取呢?我上面的写法有错
string contents=dr["列的名称"].ToString();如果是多条的话,要循环这个DataTable或者是DataSet,取得的方式是一样的
protected void Page_Load(object sender, EventArgs e)
{
_connstring = WebConfigurationManager.ConnectionStrings["proconnectionstring"].ConnectionString;
SqlConnection con = new SqlConnection(_connstring);
//取得连接中的proid值
int proid=-1;
if (Request.QueryString["proid"] != null)
{
proid = int.Parse(Request.QueryString["proid"].ToString());
}
else
{
Response.Write("ID不能为空!");
Response.End();
}
//通过proid 值查找对应记录,绑定到dataset SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select *from pro where proid=@proid";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@proid", proid);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet dst = new DataSet();
using (con)
{
sda.Fill(dst);
} // 读取dataset中的2个字节,赋值给变量,设定dropdown1 和dropdown2 选定值为对应的值
DataRow dr = dst.Tables[0].Rows[0];
string dropdown1text = dr["categoryname"].ToString();
string dropdown2text = dr["brandname"].ToString(); DropDownList drop1 = FormView1.FindControl("DropDownList1") as DropDownList;
drop1.SelectedValue=dropdown1text; // 这个地方怎么做,出问题了
DropDownList drop2 = FormView1.FindControl("DropDownList2") as DropDownList;
drop2.SelectedValue = dropdown2text; // 绑定到formview1 控件 FormView1.DataSource = dst;
FormView1.DataBind();
}