我在我的网站上使用了3层架构。我不想在gridview中更新,而是希望将gridview行值发送到其他页面并在那里执行更新操作。以下是视图页面中的链接代码。
<asp:TemplateField>
<ItemTemplate>
<a href ='<%#"UpdateCategory.aspx?CategoryID="+DataBinder.Eval(Container.DataItem,"CategoryID") %>'> <%#Eval("CategoryName") %> </a>
</ItemTemplate>
</asp:TemplateField>更新页面中的PageLoad事件:
protected void Page_Load(object sender, EventArgs e)
{
int categoryId = 0;
categoryId = Convert.ToInt32(Request.QueryString["CategoryID"]);
CategoryBL.GetCategoryByID(categoryId);
}CategoryBL代码:
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代码:
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;
}
}更新页面:
<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 />有人能帮我吗?
发布于 2016-08-31 14:44:33
您的函数等待DataTable对象返回,而您返回的是Category对象。您必须选择一个或另一个来解决错误问题。将函数类型更改为Category或选择返回dt。
选择取决于您在Page_Load函数中的处理方式。现在你的代码什么也没做,它只是创建了对象,但是给它分配了nowhere.Have。你决定如何处理更新页面中的结果?
https://stackoverflow.com/questions/39241847
复制相似问题