如何在网格视图中显示数据 [英] Howto display data in grid view
本文介绍了如何在网格视图中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是.net的新手。在我的项目中,我有一个按钮和一个文本框,用户可以在其中单击按钮时输入他的预订ID我想在网格视图中显示特定行我的代码来搜索结果如下所示。
我尝试过:
i am new to .net .in my project i have one button and one text box in which the user can enter his booking id when he click button i want to display the particular row in grid view my code to search the result is given below.
What I have tried:
protected void Button1_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from [booking] where [BookingId]='" + TextBox1.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 1)
{
}
else
{
Response.Write("&alert;script>alert('this not valid booking id')&alert;/script>");
}
}
}
推荐答案
string bookingId = TextBox1.Text;
string connString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from booking where BookingId = @BookingId";
cmd.Parameters.AddWithValue("@BookingId", bookingId)
cmd.Connection = con;
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
不要将UI中的值直接传递给查询。防止Sql注入)
Don't ever pass a value from your UI directly to your query.(To prevent Sql injection)
你好。
这篇文章&示例可能对您有所帮助:
SQL浏览器 [ ^ ]
这篇关于如何在网格视图中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文