如何在3层应用程序中的文本框中显示记录 [英] How Do I Display Records In Textbox In 3-Tier Application

查看:93
本文介绍了如何在3层应用程序中的文本框中显示记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在DAL编写的代码

Below is the code I was written in DAL

public DataSet LoginDisplay()
      {
          SqlCommand cmd = new SqlCommand("GetUser",con);
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("@UserID", objBOL.User);
          DataSet ds = new DataSet();
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          try
          {  da.Fill(ds);
              return ds;
          }
          catch
          {throw;
          }

          finally
          { da.Dispose();
              con.Close();
              con.Dispose();
          }
      }



===================== ===========================

以下是我用BLL编写的代码


================================================
Below is the code I was written in BLL

public DataSet RetrieveRecords()
      {
         LoginDAL _LoginDAL = new LoginDAL();

          try
          {return _LoginDAL.LoginDisplay();
          }
          catch
          {throw;
          }
          finally
          {_LoginDAL = null;
          }
      }



========================= ================

下面是代码 - UI


=========================================
Below is the code - UI

protected void Button1_Click(object sender, EventArgs e)
        {
           LoginBOL objBOL = new LoginBOL();
            objBOL.User = txtTest.Text;
            LoginBLL objBLL = new LoginBLL();
            DataSet ds = new DataSet();
            ds = objBLL.RetrieveRecords();
            txtRoles.Text = Convert.ToString(ds.Tables[0].Rows[0]["ROLE"]);
            txtUserID.Text= Convert.ToString(ds.Tables[0].Rows[0]["UserID"]);
            txtName.Text= Convert.ToString(ds.Tables[0].Rows[0]["EMPLOYEE_NAME"]);
}





运行代码时显示以下错误


$ b / b中的$ b服务器错误。



对象引用未设置为对象的实例。





请帮忙!。



While running the code shows the below error

Server Error in/ Application.

Object reference not set to an instance of an object.


Please help!.

推荐答案

首先你要检查DataSet是否包含从数据库返回的任何表,如果是表包含或不包含任何行。



然后我们需要检查是否存在具有值的各列。

这些检查可以是添加如下: -



First of all you need to check whether the DataSet contains any tables returned from database then if the table contains any row or not.

Then we need to check if for the individual columns having values existing or not.
These checks can be added as follows :-

if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                if(ds.Tables[0].Rows[0]["ROLE"] != null && !ds.Tables[0].Rows[0].IsNull("ROLE"))
                    txtRoles.Text = Convert.ToString(ds.Tables[0].Rows[0]["ROLE"]);
                if (ds.Tables[0].Rows[0]["UserID"] != null && !ds.Tables[0].Rows[0].IsNull("UserID"))
                    txtUserID.Text = Convert.ToString(ds.Tables[0].Rows[0]["UserID"]);
                if (ds.Tables[0].Rows[0]["EMPLOYEE_NAME"] != null && !ds.Tables[0].Rows[0].IsNull("EMPLOYEE_NAME"))
                    txtName.Text = Convert.ToString(ds.Tables[0].Rows[0]["EMPLOYEE_NAME"]);
            }





希望这个例子有助于避免你在这里遇到的错误。然后调试将帮助您检查无法满足解决条件的位置。



Hope this example will be useful to avoid the error you are getting here. Then debugging will help you to check where it is failing to satisfying the conditions to solve it.


这篇关于如何在3层应用程序中的文本框中显示记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆