Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to display the rows where class=classno where classno is taken from previous page and displayed here as a label .

The code is bellow:

What I have tried:

Class_detail.aspx
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Class_Details.aspx.cs" Inherits="Websearch.Class_Details" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <hr />
             <asp:Label ID="LabelClass" runat="server" Text="class Details" BorderColor="White"></asp:Label>
           
            <hr /> 
            <br />
            <br />
            <asp:GridView ID="GridView3" runat="server"></asp:GridView>
        </div>

    </form>
</body>
</html>
Class_details.aspx.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace Websearch
{
    public partial class Class_Details : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String MainCon = 
           ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
            SqlConnection sqlcon = new SqlConnection(MainCon);
            sqlcon.Open();
            LabelClass.Text = Request.QueryString["Classno"].ToString();
            int Classnum = Convert.ToInt32(LabelClass.Text);
            SqlCommand sqlcom1 = new SqlCommand();
            String sqlquerry1 = "Select Class, Item from [customer_class_item] where 
            Class = " + Classnum ;
            sqlcom1.CommandText = sqlquerry1;
            sqlcom1.Connection = sqlcon;
            sqlcom1.Parameters.AddWithValue("Class", LabelClass.Text);
            DataTable dt1 = new DataTable();
            SqlDataAdapter sd1 = new SqlDataAdapter(sqlcom1);
            sd1.Fill(dt1);
            GridView3.DataSource = dt1;
            GridView3.DataBind();
Posted
Updated 15-Feb-21 22:00pm
v2
Comments
Maciej Los 15-Feb-21 6:31am    
If classNo is a text, why do you convert it to int?
int Classnum = Convert.ToInt32(LabelClass.Text);
Richard Deeming 16-Feb-21 4:04am    
Whilst in this specific case you're probably safe, since Classnum is an integer, using string concatenation to build a query can and will lead to SQL Injection[^] vulnerabilities, and massive fines when your database is breached.

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

using (SqlConnection sqlcon = new SqlConnection(MainCon))
using (SqlCommand sqlcom1 = new SqlCommand("SELECT Class, Item FROM [customer_class_item] WHERE Class = @Class", sqlcon))
{
    sqlcom1.Parameters.AddWithValue("@Class", Classnum);
    
    DataTable dt1 = new DataTable();
    SqlDataAdapter sd1 = new SqlDataAdapter(sqlcom1);
    sd1.Fill(dt1);
    
    GridView3.DataSource = dt1;
    GridView3.DataBind();
}

1 solution

You can't. You are sending non-numeric data to a numeric field, so SQL is trying to convert it to an integer and rightly failing: "58XX" is not a number.

If you want to send data to numeric fields you need to validate it first, and tell the user when he made a mistake instead of blindly assuming he can type!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900