我尝试在客户数据库表格中显示GridView
数据,并按用户的电子邮件进行过滤。我试着在我的sql查询中设置条件如下:
Select * from customers where cid= profile_label.Text
但adapt.Fill(dt)
导致错误,代码无法编译。任何建议/帮助将受到高度赞赏!
这是我加载页面时执行的代码:
protected void Page_Load(object sender, EventArgs e)
{
profile_Label.Text = Session["Profile"].ToString();
Object pro = profile_Label.Text;
//Connection String from web.config File
if (!IsPostBack)
{
ShowData();
}
}
string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con;
SqlDataAdapter adapt;
DataTable dt;
以下是我的代码片段(脚本和c#)
这是我的c#代码,其中包含基于sql查询显示数据的方法
protected void ShowData()
{
dt = new DataTable();
con = new SqlConnection(cs);
con.Open();
adapt = new SqlDataAdapter("Select * from customers", con);
adapt.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}
这是我的脚本代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="6" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Height="226px" Width="1172px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" Text="Edit"
CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update" runat="server" Text="Update"
CommandName="Update"/>
<asp:Button ID="btn_Cancel" runat="server" Text="Cancel"
CommandName="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer ID">
<ItemTemplate>
<asp:Label ID="lbl_CID" runat="server"
Text='<%#Eval("CID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lbl_FName" runat="server"
Text='<%#Eval("FName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="fname_txt"
runat="server"Text='<%#Eval("FName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lbl_Lname" runat="server" Text='<%#Eval("Lname") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lname_txt" runat="server" Text='<%#Eval("Lname") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of Birth">
<ItemTemplate>
<asp:Label ID="lbl_DOB" runat="server" Text='<%#Eval("DOB") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="dob_txt" runat="server" Text='<%#Eval("DOB") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lbl_email" runat="server" Text='<%#Eval("Email") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="email_txt" runat="server" Text='<%#Eval("email") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
答案 0 :(得分:1)
您可以在查询中使用参数并将其替换为正确的值。
string query = "Select * from customers where cid = @cid";
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
{
adapter.SelectCommand.Parameters.Add("@cid", SqlDbType.Int).Value = profile_label.Text;
try
{
adapter.Fill(dt);
}
catch
{
}
}
答案 1 :(得分:0)
答案是直接将会话放入SQL查询中,而不将会话值存储在另一个变量中
String query = "SELECT * FROM customers WHERE Email ='"+ Session["Profile"]+"'";