1

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. enter image description here

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).

Community
  • 1
  • 1
BrunoEarth
  • 333
  • 2
  • 5
  • 16
  • 4
    Presumably the result is DBNull, you need the compare it against DBNull.Value and go from there. You need to use SqlParamaters *not* string.Format in order to avoid SQL Injection. – Alex K. May 11 '17 at 14:37
  • 1
    You should probably test the return value for null before assigning it to an `Int32`. Could be no rows were returned, for example. – John Wu May 11 '17 at 14:38
  • 1
    If you execute that bit of sql in a SSMS window, what is the value of `rn`? – Jamiec May 11 '17 at 14:38
  • 1
    Before you do anything else you need to read about, understand and start using parameterized queries before bobby tables comes to visit. http://bobby-tables.com/ – Sean Lange May 11 '17 at 14:41
  • In addition to Bobby tables please read [MCVE] guidance on posting code. There is no value in posting more code than necessary (especially if that code demonstrates worst possible practices). – Alexei Levenkov May 11 '17 at 14:56
  • You realize that your `LIKE` comparison without placeholders is just an equlas(=) comparison? – Mad Myche May 11 '17 at 15:38

1 Answers1

5

The ROW_NUMBER window function returns a bigint.

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

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77