Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, i want to retrieve data from sql server but my code are not functioning. It doesn't show any error. But it still cannot display the data. Can someone help me. Thanks

What I have tried:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Odbc;
using System.Collections.Specialized;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;


public partial class EditStatus : System.Web.UI.Page
{
string connectionString = @"Data Source=P13L1TNN225\SQLEXPRESS; Initial Catalog = EMPLOYEE1; Integrated Security=True;";
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();


protected void Page_Load(object sender, EventArgs e)
{



using (SqlConnection con = new SqlConnection(connectionString))
{
string query = "Select * from Ticket where TicketID='" + TicketID.Text + "'";

using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();

SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
TicketID.Text = dr["TicketID"].ToString();
lblEmpID.Text = dr["Emp_id"].ToString();
lblEmpName.Text = dr["Emp_Name"].ToString();
lblEmpEmail.Text = dr["Emp_Email"].ToString();
lblCnum.Text = dr["Emp_Cnum"].ToString();
lblDpt.Text = dr["Emp_Dpt"].ToString();
lblPlant.Text = dr["Emp_Plant"].ToString();
lblSeverity.Text = dr["Emp_Severity"].ToString();
lblSubject.Text = dr["Emp_Subject"].ToString();
lblDscri.Text = dr["Emp_Dscri"].ToString();
lblStatus.Text = dr["Emp_Status"].ToString();
lblDatetime.Text = dr["Datetime"].ToString();

}
con.Close();
con.Dispose();

}

}
Posted
Updated 2-May-18 20:29pm

Two things:
1) It doesn't display any data because no rows match. Check what - exactly - you have in TicketID.Text and then manually look through your database for that exact string. For an "=" comparison to succeed, the strings must match exactly, including any whitespace.

2) Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
You wrote the code on the page load event, please debug and make sure that you actually have some data in "TicketID.Text". If not, call this code on some other event after you have given the data to TicketID, possibly on a Button click event.

I also suggest you get a good understanding of the ASP.Net page lifecycle (you will find some good articles on CP).

Another point, It is not a good idea to write SQL queries the way you have written.

This is called concatenated SQL queries and they introduce a vulnerability towards SQL injection.
Suppose, a user enters the following text in the textbox TicketID "56; drop table XYZ;"
Now when you concatenate this value in the line SQL will receive two instructions, one is to get the data for the given ticket id and the other is to delete the table XYZ.
That is a serious issue, you do not want users to be able to delete tables from your database.

A better approach is to write parametrized SQL queries. Read more about it here.
 
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