【问题标题】:Populate my text box based on the my dataset using asp.net c#使用asp.net c#根据我的数据集填充我的文本框
【发布时间】:2015-06-26 00:18:06
【问题描述】:

我想根据我的数据集填充我的文本框。

DAL

public static DataTable GetCustomer(collection b)
{
    {
        DataTable table;
        try
        {
            string returnValue = string.Empty;
            DB = Connect();
            DBCommand = connection.Procedure("getCustomer");
            DB.AddInParameter(DBCommand, "@CustomerRef", DbType.String, b.CustomerRef1);

            DbDataReader reader = DBCommand.ExecuteReader();
            table = new DataTable();
            table.Load(reader);
            return table;
        }
        catch (Exception ex)
        {
            throw (ex);
        }

    }

}

BLL

   public DataSet returnCustomer(collection b)
   {
       try
       {
           SqlDataAdapter adapt = new SqlDataAdapter();
           DataSet table = new DataSet();

           adapt.Fill(table, "table");
           return table;
       }
       catch (Exception ex)
       {
           throw ex;
       }
   }

PL

protected void ddl_Customers_SelectedIndexChanged(object sender, EventArgs e)
{

    DAL.collection cobj = new collection();
    BLL.business bobj = new business();

    string selectedValue = ddl_Customers.SelectedValue.ToString();

    //populate the text boxes
    txtCustomerRef.Text = bobj.returnCustomer(cobj);
}

我收到一个错误:

无法将类型“System.Data.DataSet”隐式转换为“字符串”

当我填充我的文本框时,我需要从数据集中获取特定值,这应该是客户参考。但我看不出我做错了什么?

【问题讨论】:

  • 您的 returnCustomer 返回 DataSet 但您尝试将其返回值分配给 txtCustomerRef.Text 这是一个 string 并且这两种类型之间没有隐式对话。考虑改变你的逻辑。
  • @SonerGönül:您能否建议更改答案?

标签: c# asp.net webforms data-access-layer 3-tier


【解决方案1】:

您不能将dataset 设置为textbox 的文本属性,因为它只接受字符串。

假设数据集的第一个表的第一行和第一列具有所需的值,更改将如下所示。

txtCustomerRef.Text = bobj.returnCustomer(cobj).Tables[0].Rows[0][0].ToString();

还需要在执行ToString之前添加null检查。

【讨论】:

  • 从下拉列表中选择值时出现错误:The SelectCommand property has not been initialized before calling 'Fill'.
  • 这对我来说似乎不正确,它只返回空数据集,您需要修复它并使用我的代码。
  • 我添加了adapt.SelectCommand = cmd;,但不确定如何定义 sql 命令,因为我使用的是 DAL,虽然 SQL 命令需要一个参数,而这在 DAL...
  • 我想,原来的问题已经偏离了它的实际错误场景,你需要问问 DAL 设计师,谁创造了这个。
  • 不接受您的答案是正确的,只是认为您可以帮助解决两件事
猜你喜欢
  • 2014-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-29
  • 2021-12-04
  • 1970-01-01
相关资源
最近更新 更多