使用asp.net指定的强制转换无效错误? [英] Specified cast is not valid error using asp.net?

查看:52
本文介绍了使用asp.net指定的强制转换无效错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是尝试执行我的网站后出现的错误.这与我先前的问题有关,但这次的目标不同.如果您想参考该链接,请点击这里.

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

 使用系统;使用System.Collections.Generic;使用System.Configuration;使用System.Data;使用System.Data.SqlClient;使用System.Linq;使用System.Web;使用System.Web.UI;使用System.Web.UI.WebControls;公共局部类_Default:System.Web.UI.Page{SqlConnection con,con1;SqlCommand cmd,cmd1;数据集ds,ds1;private int _x;公共诠释X{得到{return _x;}设置{_x =值;}}公共_Default(){con =新的SqlConnection();con.ConnectionString = ConfigurationManager.ConnectionStrings ["GuitarItemsDBConnectionString2"].ToString();cmd =新的SqlCommand();ds = new DataSet();}受保护的void Page_Load(对象发送者,EventArgs e){如果(!IsPostBack){bindgridviewguitaritems();}}public void getRowNumber(字符串品牌,字符串模型){字符串查询= 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}'",品牌,型号);con.Open();cmd.Connection = con;cmd.CommandText =查询;Int32计数=(Int32)cmd.ExecuteScalar();//< ----这是错误X =计数;con.Close();}//开始吉他项目的Gridview代码私人无效bindgridviewguitaritems(){con.Open();cmd.CommandText ="SELECT * FROM [guitarItems]";cmd.Connection = con;SqlDataAdapter da =新的SqlDataAdapter(cmd);da.Fill(ds);con.Close();GridView1.DataBind();}受保护的void GridViewBtn1_Click(对象发送者,EventArgs e){Button btn =发送者为Button;GridViewRow gridrow = btn.NamingContainer为GridViewRow;int id = Convert.ToInt32(GridView1.DataKeys [gridrow.RowIndex] .Value.ToString());字符串名称= GridView1.Rows [gridrow.RowIndex] .Cells [3] .Text;字符串模型= GridView1.Rows [gridrow.RowIndex] .Cells [4] .Text;getRowNumber(name,model);Label1.Text = X.ToString();Label2.Text =名称;Label3.Text =模型;con.Open();cmd.CommandText =从[guitarItems]删除,其中id =" + id;cmd.Connection = con;int a = cmd.ExecuteNonQuery();con.Close();如果(a> 0){bindgridviewguitaritems();}System.IO.File.Delete(@"C:\ Users \ User1 \ Documents \ Visual Studio 2015 \ WebSites \ MusicStore \ Pages \ GuitarItems" +名称+"Details" + id +".aspx");System.IO.File.Delete(@"C:\ Users \ User1 \ Documents \ Visual Studio 2015 \ WebSites \ MusicStore \ Pages \ GuitarItems" +名称+"Details" + id +".aspx.cs");}//吉他项目的Gridview代码结尾 

请随时为该问题提出替代解决方案.顺便说一句,这是只是一个用于测试事物的小程序.如果一切顺利,我可以将其中的一些合并到我的实际项目中(未参数化的方面除外).

解决方案

ROW_NUMBER 窗口函数文档,要使用的相应.NET数据类型是 Int64 .

here is the error that came up after attempting to execute my website. This is related to my previous question but different objective this time. Here is the link if you want to refer back to it. How to code a nested sql statement to get row number of a specific item in mssql?

Below is an image of the server error.

Its referring to the Int32 count = (Int32)cmd.ExecuteScalar(); that i wrote in my code. Here is the full code. I have also added a comment to where the error originated.

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

Feel free to suggest alternative solutions to this problem. By the way, this is only a small program for testing out things. If all goes well, I might incorporate some of it in my actual project(except the aspects where it is not parameterized).

解决方案

The ROW_NUMBER window function returns a bigint.

According to documentation, the corresponding .NET data type to use is the Int64.

这篇关于使用asp.net指定的强制转换无效错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆