数据不在表单中获取 [英] data is not fetch in the form
问题描述
protected void BtnCase_Click(object sender,EventArgs e)
{
if(!(IsPostBack))
{
if(Request.QueryString.HasKeys()&& Request.QueryString [id]。ToString()!= null)
{
SqlConnection sc = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings [hospitalmanagementdb]。ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText =select * from patient where patient_id =+ Request.QueryString [id]。ToString();
cmd.Connection = sc;
sc.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
sc .Close();
txtFirstnm.Text = sdr [1] .ToString();
txtLastnm.Text = sdr [2] .ToString();
RadioMF.Text = sdr [3] .ToString();
txtAge.Text = sdr [4] .ToString();
txtContact.Text = sdr [5] .ToString();
txtEmail.Text = sdr [6] .ToString();
txtAddress.Text = sdr [7] .ToString();
}
}
}
没有错误但是表中的数据无法以表格形式显示
protected void BtnCase_Click(object sender, EventArgs e)
{
if (!(IsPostBack))
{
if (Request.QueryString.HasKeys() && Request.QueryString["id"].ToString() != null)
{
SqlConnection sc = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["hospitalmanagementdb"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from patient where patient_id= " + Request.QueryString["id"].ToString();
cmd.Connection = sc;
sc.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
sc.Close();
txtFirstnm.Text = sdr[1].ToString();
txtLastnm.Text = sdr[2].ToString();
RadioMF.Text = sdr[3].ToString();
txtAge.Text = sdr[4].ToString();
txtContact.Text = sdr[5].ToString();
txtEmail.Text = sdr[6].ToString();
txtAddress.Text = sdr[7].ToString();
}
}
}
there is no error but data from table can not be display in form
推荐答案
你怎么知道没有错误?你没有检查...
最不可能的原因是它没有显示任何一项测试:
IsPostBack可能是真的。
HasKeys可以返回false - 没有键。
它们可能不是查询字符串id。
在这种情况下,我怀疑IsPostBack,因为按钮点击事件处理程序总是如此...
How do you know there is no error? You aren't checking...
The most likely reasons for it not displaying anything are that it failed one of your tests:
IsPostBack could be true.
HasKeys could return false - there are no keys.
They may not be a query string "id".
In this case, I suspect IsPostBack, since it will always be true for a button click event handler...
你需要删除:
You need to remove:
if (!(IsPostBack))
{
}
这就是你的代码没有运行的原因。
将您的查询转换为此:
That is why your code is not running.
Change your query to this:
cmd.CommandText = "select * from patient where patient_id= '" + Request.QueryString["id"].ToString() + "'";
只要您在查询字符串中实际传递id,这将有效。
This will work provided you are actually passing "id" in the query string.
protected void BtnCase_Click(object sender, EventArgs e)
{
if (!(IsPostBack))
{
if (Request.QueryString.HasKeys() && Request.QueryString["id"].ToString() != null)
{
SqlConnection sc = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["hospitalmanagementdb"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from patient where patient_id= " + int.Parse(Request.QueryString["id"].ToString()); // or .... patient_id = '" + Request.QueryString["id"]+ "'";
cmd.Connection = sc;
sc.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
sc.Close();
txtFirstnm.Text = sdr[1].ToString();
txtLastnm.Text = sdr[2].ToString();
RadioMF.Text = sdr[3].ToString();
txtAge.Text = sdr[4].ToString();
txtContact.Text = sdr[5].ToString();
txtEmail.Text = sdr[6].ToString();
txtAddress.Text = sdr[7].ToString();
}
}
}
-KR
-KR
这篇关于数据不在表单中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!