gpt4 book ai didi

c# - Specified cast is not valid error 使用asp.net?

转载 作者:太空宇宙 更新时间:2023-11-03 19:47:17 25 4
gpt4 key购买 nike

这是尝试执行我的网站后出现的错误。这与我之前的问题有关,但这次的目标不同。如果你想回头引用,这里是链接。 How to code a nested sql statement to get row number of a specific item in mssql?

下面是服务器错误的图像。 enter image description here

它指的是 Int32 count = (Int32)cmd.ExecuteScalar();我在我的代码中写的。这是完整的代码。我还对错误的来源添加了评论。

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
SqlConnection con, con1;
SqlCommand cmd, cmd1;
DataSet ds, ds1;
private int _x;
public int X
{
get { return _x; }
set { _x = value; }
}
public _Default()
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["GuitarItemsDBConnectionString2"].ToString();
cmd = new SqlCommand();
ds = new DataSet();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
bindgridviewguitaritems();
}
}

public void getRowNumber(string brand, string model)
{
string query = string.Format("SELECT Rn FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY id) AS Rn FROM guitarItems WHERE brand LIKE '{0}') x WHERE x.Model LIKE '{1}'",brand,model);
con.Open();
cmd.Connection = con;
cmd.CommandText = query;
Int32 count = (Int32)cmd.ExecuteScalar(); //<----Here is the error
X = count;
con.Close();

}

//Start of Gridview Code for Guitar Items
private void bindgridviewguitaritems()
{
con.Open();
cmd.CommandText = "SELECT * FROM [guitarItems]";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
GridView1.DataBind();
}



protected void GridViewBtn1_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
GridViewRow gridrow = btn.NamingContainer as GridViewRow;
int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString());
string name = GridView1.Rows[gridrow.RowIndex].Cells[3].Text;
string model = GridView1.Rows[gridrow.RowIndex].Cells[4].Text;
getRowNumber(name,model);
Label1.Text = X.ToString();
Label2.Text = name;
Label3.Text = model;
con.Open();
cmd.CommandText = "DELETE FROM [guitarItems] WHERE id=" + id;
cmd.Connection = con;
int a = cmd.ExecuteNonQuery();
con.Close();
if (a > 0)
{
bindgridviewguitaritems();
}
System.IO.File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + name + "Details" + id + ".aspx");
System.IO.File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + name + "Details" + id + ".aspx.cs");
}

//End of Gridview Code for Guitar Items

请随时提出此问题的替代解决方案。顺便说一下,这是只是一个测试东西的小程序。如果一切顺利,我可能会在我的实际项目中加入一些(除了没有参数化的方面)。

最佳答案

ROW_NUMBER 窗口函数 returns a bigint .

根据 documentation ,要使用的相应 .NET 数据类型是 Int64

关于c# - Specified cast is not valid error 使用asp.net?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43918500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com