【问题标题】:Specified cast is not valid error using asp.net?使用 asp.net 指定的强制转换无效错误?
【发布时间】:2017-05-11 14:35:12
【问题描述】:

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

下面是服务器错误的图像。

它指的是 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

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

【问题讨论】:

  • 大概结果是 DBNull,您需要将它与 DBNull.Value 进行比较,然后从那里开始。您需要使用 SqlParamaters not string.Format 以避免 SQL 注入。
  • 您可能应该在将返回值分配给Int32 之前测试 null 的返回值。例如,可能没有返回任何行。
  • 如果在SSMS窗口中执行那段sql,rn的值是多少?
  • 在您做任何其他事情之前,您需要阅读、理解并开始使用参数化查询,然后才能访问 bobby 表。 bobby-tables.com
  • 除了 Bobby 桌子,请阅读minimal reproducible example 发布代码指南。发布不必要的代码没有任何价值(特别是如果该代码展示了最糟糕的做法)。

标签: c# asp.net sql-server


【解决方案1】:

ROW_NUMBER 窗口函数returns a bigint

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

【讨论】:

  • 是的,这可能是问题所在。他也可以把SQL改成SELECT CAST(Rn AS INT) as Rn FROM...或者把代码改成Int64
  • 宾果游戏!你绝对做到了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多