如何在以下查询中使用datavaluefield值作为参数值 [英] How do I use datavaluefield value as the parameter value in following query
本文介绍了如何在以下查询中使用datavaluefield值作为参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我绑定到dropdownlist,并尝试将datavalue用作以下查询中的参数以绑定到gridview。任何帮助,将不胜感激。代码如下。
我尝试过:
public partial class ConsolidatedReport:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
BindCustomersDDL();
}
private void BindCustomersDDL()
{
DataTable dt = new DataTable();
使用(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ AMTConnectionString]。ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand( 选择*来自dbo.customer的订单,按2,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
ddlCustomer.DataSource = dt;
ddlCustomer.DataTextField = cname; // 您要在下拉列表中显示的内容
ddlCustomer.DataValueField = customerid;
ddlCustomer.DataBind();
}
受保护 void btnExecute_Click( object sender,EventArgs e)
{
BindBacteriaGrid();
}
private void BindBacteriaGrid()
{
DataTable dt = new DataTable();
try
{
SqlConnection con = new SqlConnection(ConfigurationManager。 ConnectionStrings [ AMTConnectionString]。ConnectionString);
SqlDataAdapter adp = new SqlDataAdapter( SELECT [CustomerID],WellID,SRB_RISK,来自[AMT]的APB_RISK。[dbo] .bacteria其中customerid = @cust order by 1,con);
adp.SelectCommand.Parameters.AddWithValue( @ cust,ddlCustomer.Text);
adp.Fill(dt);
if (dt.Rows.Count > 0 )
{
BacteriaGridview.DataSource = dt;
BacteriaGridview.DataBind();
}
else
{
BacteriaGridview.DataSource = null 跨度>;
BacteriaGridview.DataBind();
}
}
catch (例外情况)
{
Response.Write( 错误发生: + ex.ToString());
}
最后
{
dt.Clear();
dt.Dispose();
}
}
解决方案
Page.IsPostback
缺失
受保护 void Page_Load( object sender,EventArgs e)
{
if (Page .IsPostBack) return ;
BindCustomersDDL();
}
用于阅读下拉选项,使用
var text = ddlCustomer.SelectedItem.Text;
var value = ddlCustomer.SelectedItem.Value;
I bind to dropdownlist, and am trying to use the datavalue as the parameter in my following query to bind to gridview. any help would be appreciated. Code below.
What I have tried:
public partial class ConsolidatedReport : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindCustomersDDL();
}
private void BindCustomersDDL()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AMTConnectionString"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from dbo.customer order by 2", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
ddlCustomer.DataSource = dt;
ddlCustomer.DataTextField = "cname";//Which you want to display inthe dropdownlist
ddlCustomer.DataValueField = "customerid";
ddlCustomer.DataBind();
}
protected void btnExecute_Click(object sender, EventArgs e)
{
BindBacteriaGrid();
}
private void BindBacteriaGrid()
{
DataTable dt = new DataTable();
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AMTConnectionString"].ConnectionString);
SqlDataAdapter adp = new SqlDataAdapter("SELECT [CustomerID], WellID, SRB_RISK, APB_RISK from [AMT].[dbo].bacteria where customerid = @cust order by 1", con);
adp.SelectCommand.Parameters.AddWithValue("@cust", ddlCustomer.Text);
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
BacteriaGridview.DataSource = dt;
BacteriaGridview.DataBind();
}
else
{
BacteriaGridview.DataSource = null;
BacteriaGridview.DataBind();
}
}
catch (Exception ex)
{
Response.Write("Error Occured: " + ex.ToString());
}
finally
{
dt.Clear();
dt.Dispose();
}
}
解决方案
Page.IsPostback
is missing
protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) return; BindCustomersDDL(); }
for reading the dropdown selected item, use
var text = ddlCustomer.SelectedItem.Text; var value = ddlCustomer.SelectedItem.Value;
这篇关于如何在以下查询中使用datavaluefield值作为参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文