如何从SQL中的两个表中检索数据并在ASP网页中填充文本框?

时间:2018-06-15 16:58:32

标签: c# sql asp.net

USE [Apartmentmanagementsystem]
GO
/****** Object: StoredProcedure [dbo].[userview] Script Date: 6/15/2018 7:52:10 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[userview]
@tenant_id int

as 
BEGIN 

    select * 
    from Tenant  
    join Lease on Tenant.tenant_id=Lease.tenant_id 
    where Tenant.tenant_id=@tenant_id;

END

这是我的存储过程。

C#代码是

SqlConnection constring = new SqlConnection(@"Data Source=MOSH-PC\SQLEXPRESS;Initial Catalog=Apartmentmanagementsystem;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                clear();


                   if (!String.IsNullOrEmpty(Request.QueryString["id"]))
                {

                    int tenant_id = Convert.ToInt32(Request.QueryString["id"]);
                    if (constring.State == ConnectionState.Closed)
                    {
                        constring.Open();
                        SqlDataAdapter sqda = new SqlDataAdapter("userview", constring);
                        sqda.SelectCommand.CommandType = CommandType.StoredProcedure;
                        sqda.SelectCommand.Parameters.AddWithValue("@tenant_id", tenant_id);
                        DataTable dtbl = new DataTable();
                        sqda.Fill(dtbl);
                        hftenant_id.Value = tenant_id.ToString();
                        if (dtbl.Rows.Count > 0)
                        {
                            txtFName.Text = dtbl.Rows[0][1].ToString();
                            txtLName.Text = dtbl.Rows[0][2].ToString();
                            txtPhone.Text = dtbl.Rows[0][3].ToString();
                            txtEmail.Text = dtbl.Rows[0][4].ToString();
                            txtAge.Text = dtbl.Rows[0][5].ToString();
                            ddlGender.Items.FindByValue(dtbl.Rows[0][6].ToString()).Selected = true;
                            txtSdate.Text = dtbl.Rows[0][7].ToString();
                            txtEdate.Text = dtbl.Rows[0][8].ToString();
                            txtDeposit.Text = dtbl.Rows[0][9].ToString();
                        }

                    }
                }
            }

        }
        public void clear()
        {
            txtFName.Text = txtLName.Text = txtPhone.Text = txtEmail.Text = txtAge.Text = ddlGender.Text = txtSdate.Text = txtEdate.Text = txtDeposit.Text = "";
            hftenant_id.Value = "";
        }
        protected void btnSave1_Click(object sender, EventArgs e)
        {

            try
            {
                if (constring.State == ConnectionState.Closed)
                    constring.Open();
                SqlCommand sqlcmd = new SqlCommand("SPnew1", constring);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.AddWithValue("@tenant_id", Convert.ToInt32(hftenant_id.Value == "" ? "0" : hftenant_id.Value));
                sqlcmd.Parameters.AddWithValue("@F_Name", txtFName.Text.Trim());
                sqlcmd.Parameters.AddWithValue("@L_Name", txtLName.Text.Trim());
                sqlcmd.Parameters.AddWithValue("@Phone", txtPhone.Text.Trim());
                sqlcmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                sqlcmd.Parameters.AddWithValue("@Age", txtAge.Text.Trim());
                sqlcmd.Parameters.AddWithValue("@Gender", ddlGender.SelectedValue);
                sqlcmd.Parameters.AddWithValue("@lease_id", 0);
                sqlcmd.Parameters.AddWithValue("@initial_date", txtSdate.Text.Trim());
                sqlcmd.Parameters.AddWithValue("@end_date", txtEdate.Text.Trim());
                sqlcmd.Parameters.AddWithValue("@deposit", txtDeposit.Text.Trim());
                sqlcmd.ExecuteNonQuery();
                clear();

            }

            catch (Exception ex)
            {

            }
            finally
            {
                constring.Close();
            }

问题在于,当我输入id时,会显示示例?id=1,但Tenant表会将lease的内容推送到位。

我认为问题是下拉列表。

如何从两个SQL表中检索数据并填充ASP网页中的文本框?

1 个答案:

答案 0 :(得分:0)

问题是UI ..您需要发送aspx代码或将代码添加到GIT存储库。 首先......使用html表

 <table>
  <tr>
    <td>Gender: </td>
    <td> <asp:dropdownlist....></asp:dropdownlist> </td>
    <td>other : </td>
    <td> <asp:textbox..some other control..></asp:dropdownlist> </td>
  </tr>
  <tr>
    <td colspan="4">your data grid / html shoudl go here to display data from your 
    stored procedure. <td>
   </tr>
 </table>

如果您需要任何进一步的帮助,请随意添加评论,添加您的aspx页面woudl有助于为您的问题获得更具体的答案。