c# - 显示包含特定整数类型字段的最大值的整行。返回错误
问题描述
我需要一些关于使用 SQL 的帮助,我需要有 C#/SQL 知识的人,我需要的只是一行简单的代码,或者如果需要一个只有一个目的的整个代码:只显示在Gridview/Label 中的表格汽车,谁能提供我这样的代码并教我?这个问题与Web开发有关。
我已经尝试使用我将在下面提供的一些代码,但我总是收到一个错误,说明标准类型不匹配或类似的东西。
//this button is inside a masterpage.cs file
protected void Button1_Click(object sender, EventArgs e)
{
string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("App_Data/DatabaseVSC.accdb");
localhost.wbCarsDb o = new localhost.wbCarsDb();
DataSet ds = o.GetMostPopularCar(constr);
string x = (ds.Tables[0].Rows[0]["CLikes"].ToString());
DataSet ds2 = new DataSet();
ds2 = o.retDetailsCID_datasetR(x,constr);
this.GridView2.DataSource = ds2;
this.GridView2.DataBind();
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
//these are codes used in the code above inside the button, they're stored inside the main CarsDb class that I use to store all crucial codes
[WebMethod]
public DataSet GetMostPopularCar(string connectionstr)
{
string querystr = string.Format("SELECT MAX(CLikes) AS LargestLike FROM Cars");
OleDbConnection connectObj = new OleDbConnection(connectionstr);
OleDbDataAdapter da = new OleDbDataAdapter(querystr, connectObj);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
[WebMethod]
public DataSet retDetailsCID_datasetR(string CLikes, string connectionstr)
{
string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("App_Data/DatabaseVSC.accdb");
string querystr = string.Format("SELECT [CID] FROM [Cars] WHERE [CLikes]='{0}'", CLikes);
OleDbConnection connectObj = new OleDbConnection(connectionstr);
OleDbDataAdapter da = new OleDbDataAdapter(querystr, connectObj);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
这是我激活按钮时发生的情况:
列 'CLikes' 不属于表 Table。
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.ArgumentException:列 'CLikes' 不属于表 Table。
第 26 行:localhost.wbCarsDb o = new localhost.wbCarsDb();
第 27 行:数据集 ds = o.GetMostPopularCar(constr);
第 28 行:
字符串 x = (ds.Tables[0].Rows[0]["CLikes"].ToString());
第 29 行:数据集 ds2 = new DataSet(); // 第 28 行是红色的源文件:c:\Users\alaas\OneDrive\Email attachments\Documents\School\ComputerProgramming\VintageSportsCars2\MasterPage.master.cs 行:28
堆栈跟踪:
[ArgumentException: Column 'CLikes' does not belong to table Table.]
System.Data.DataRow.GetDataColumn(String columnName) +5953463
System.Data.DataRow.get_Item(String columnName) +13
MasterPage.Button1_Click(Object sender, EventArgs e) in c:\Users\alaas\OneDrive\Email attachments\Documents\School\ComputerProgramming\VintageSportsCars2\MasterPage.master.cs:28
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9782698
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
版本信息:Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.7.3282.0
解决方案
正如@Steve 在评论中回答的那样 -
您已重命名
CLikes
为LargestLike
在第一个查询中。您应该在从数据集中阅读时使用它而您的第二个查询,如果
CLikes
是一个数字列,那么您应该将其视为一个数字,而不是转换为字符串并使用字符串在数字列中查找值。
我已将此添加为社区 Wiki,以便可以解决问题而不会隐藏在评论中的答案;全部归功于@Steve。
推荐阅读
- angular - 对同一项目中的管道进行一次调用
- node.js - 你能通过 Node.js 运行 excel 宏吗
- javascript - 如何在 jquery/ajax 中中止和取消 XHR 挂起请求?
- android - 导航错误 - 此导航图未引用任何布局文件?
- javascript - 将柏树文件部署到詹金斯时如何解决“找不到模块'fs-extra' - 错误”?
- c# - 如何在代码后面通过公共路径动态地对项目进行分组?
- python - Python3 不能在 else 语句之后重复循环(谷歌搜索并没有找到可靠的结果)
- sql - ON DELETE CASCADE 用于 Sequelize 的多个外键
- c# - 如何从 webmethod 获取响应并传递 sql 参数
- python - Reverse only numbers from file