首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能将类型'Object‘隐式转换为'System.Data.DataTable’

不能将类型'Object‘隐式转换为'System.Data.DataTable’
EN

Stack Overflow用户
提问于 2016-08-31 14:03:52
回答 1查看 2K关注 0票数 0

我在我的网站上使用了3层架构。我不想在gridview中更新,而是希望将gridview行值发送到其他页面并在那里执行更新操作。以下是视图页面中的链接代码。

代码语言:javascript
复制
 <asp:TemplateField>  
             <ItemTemplate>
                <a href ='<%#"UpdateCategory.aspx?CategoryID="+DataBinder.Eval(Container.DataItem,"CategoryID") %>'> <%#Eval("CategoryName") %>  </a>
            </ItemTemplate> 
        </asp:TemplateField>

更新页面中的PageLoad事件:

代码语言:javascript
复制
protected void Page_Load(object sender, EventArgs e)
    {
        int categoryId = 0;
        categoryId = Convert.ToInt32(Request.QueryString["CategoryID"]);
        CategoryBL.GetCategoryByID(categoryId);
    }

CategoryBL代码:

代码语言:javascript
复制
public static DataTable GetCategoryByID(int categoryID)
    {
        Category category = new Category();

        string query = "SELECT * FROM [Categories] WHERE [CategoryID] = @CategoryID";
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.AddWithValue("@CategoryID", SqlDbType.Text).Value = categoryID;

        DataTable dt = DbUtility.GetRecordsInDataTable(cmd);
        if (dt.Rows.Count > 0)
        {
            category.CategoryID = Convert.ToInt32(dt.Rows[0]["CategoryID"]);
            category.CategoryName = dt.Rows[0]["CategoryName"].ToString();
            category.Description = dt.Rows[0]["Description"].ToString();

            return category;
//Cannot implicitly convert type 'Object' to 'System.Data.DataTable'
        }
        else
        {
            return null;
        }
    }

在CategoryBL页面中返回类别时出现上述错误。在这里,我想在更新页面中显示选定的类别。

GetRecordsInDataTable代码:

代码语言:javascript
复制
public static DataTable GetRecordsInDataTable(SqlCommand cmd)
    {
        DataTable dt = new DataTable();

        SqlConnection con = new SqlConnection();
        con.ConnectionString = GetConStr();   

        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = cmd;

        try
        {
            con.Open();
            adapter.SelectCommand.Connection = con;
            adapter.Fill(dt);

            return dt;
        }
        catch (Exception ex)
        {

             //Logger.Log(ex);
            throw ex;
        }


    }

更新页面:

代码语言:javascript
复制
        <asp:Label ID="Label1" runat="server" Text="Category Name"></asp:Label>
        <asp:TextBox ID="CategoryNameTextBox" runat="server"></asp:TextBox>
        <br />
        <br />

    <asp:Label ID="Label3" runat="server" Text="Description"></asp:Label>
        <asp:TextBox ID="DescriptionTextBox" runat="server"></asp:TextBox>
        <br />
        <br />

有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2016-08-31 14:44:33

您的函数等待DataTable对象返回,而您返回的是Category对象。您必须选择一个或另一个来解决错误问题。将函数类型更改为Category或选择返回dt。

选择取决于您在Page_Load函数中的处理方式。现在你的代码什么也没做,它只是创建了对象,但是给它分配了nowhere.Have。你决定如何处理更新页面中的结果?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39241847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档