首页 > 解决方案 > 显示包含特定整数类型字段的最大值的整行。返回错误

问题描述

我需要一些关于使用 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

标签: c#sqlms-access

解决方案


正如@Steve 在评论中回答的那样 -

您已重命名CLikesLargestLike在第一个查询中。您应该在从数据集中阅读时使用它

而您的第二个查询,如果CLikes是一个数字列,那么您应该将其视为一个数字,而不是转换为字符串并使用字符串在数字列中查找值。


我已将此添加为社区 Wiki,以便可以解决问题而不会隐藏在评论中的答案;全部归功于@Steve。


推荐阅读