Problem in reteriving data from database

While retreiving data from database , i'm checking 3 conditions together, but the data is not getting displayed in gridview...




 protected void Page_Load(object sender, EventArgs e)
        {
            GetResults();
        }

        private void GetResults()
        {

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HousingConnectionString"].ConnectionString);
            conn.Open();
            string query;
            SqlCommand com;
            SqlDataReader reader;

            SqlDataAdapter adapter = new SqlDataAdapter();



            query = "SELECT Flat ,First_Name , Middle_Name , Last_Name , Mobile_No , Email, DOB , Age , Education ,  Office_Add , Native_Add , PAN_Card , Aadhar_Card , Religion , Business_Job , Married_Unmarried ,  No_Of_Members , Joining_Date ,Leaving_Date from Ex_Primary_Member where Flat='" + Request.QueryString["fnum"] + "' and Joining_Date='" + Request.QueryString["jdate"] + "'  and Leaving_Date='" + Request.QueryString["ldate"] + "'";

            com = new SqlCommand(query, conn);
            adapter.SelectCommand = new SqlCommand(query, conn);

            reader = com.ExecuteReader();

            GridView1.DataSource = reader;


            GridView1.DataBind();

        }

                                          

Open in new window

Sukesh ShuklaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
You are selecting by date(s) is it possible that the precision of the stored data is higher then the precision of the parameters?

e.g.
if a stored date includes time like this 2015:10:10 12:13:14.12345
then
2015-10-10 12:13:14.12345 <> 2015-10-10

What happens if you omit the 2 date filters?

just for reference:
SELECT
      Flat
    , First_Name
    , Middle_Name
    , Last_Name
    , Mobile_No
    , Email
    , DOB
    , Age
    , Education
    , Office_Add
    , Native_Add
    , PAN_Card
    , Aadhar_Card
    , Religion
    , Business_Job
    , Married_Unmarried
    , No_Of_Members
    , Joining_Date
    , Leaving_Date
FROM Ex_Primary_Member
--where Flat='" + Request.QueryString["fnum"] + "' 
--and Joining_Date='" + Request.QueryString["jdate"] + "'  
--and Leaving_Date='" + Request.QueryString["ldate"] + "'";

Open in new window

0
Paul JacksonSoftware EngineerCommented:
Try this :

private void GetResults()
        {
            
	    SqlConnection myConn = new SqlConnection();
	    MyConn.ConnectionString = ConfigurationManager.ConnectionStrings["HousingConnectionString"].ConnectionString);

            SqlCommand MyCommand = new SqlCommand();
            MyCommand.CommandText = "SELECT Flat ,First_Name , Middle_Name , Last_Name , Mobile_No , Email, DOB , Age , Education ,  Office_Add , Native_Add , PAN_Card , Aadhar_Card , Religion , Business_Job , Married_Unmarried ,  No_Of_Members , Joining_Date ,Leaving_Date from Ex_Primary_Member where Flat='" + Request.QueryString["fnum"] + "' and Joining_Date='" + Request.QueryString["jdate"] + "'  and Leaving_Date='" + Request.QueryString["ldate"] + "'";
	    MyCommand.CommandType = CommandType.Text;
            MyCommand.Connection = MyConn;

	    DataTable MyTable = new DataTable();
	    SqlDataAdapter MyAdapter = new SqlDataAdapter();
	    MyAdapter.SelectCommand = MyCommand;
	    MyAdapter.Fill(MyTable);

            GridView1.DataSource = MyTable.DefaultView;
            GridView1.DataBind();

	    MyAdapter.Dispose();
	    MyCommand.Dispose();
	    MyConnection.Dispose();

        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DcpKingCommented:
Pick 3 common values that you expect to give a result and run the query straight on the database (in a query window in SSMS). Do you get the expected results? (yes, I _know_ that seems a silly question, but you wouldn't believe how often one writes code and testing fails ... because the right data isn't present!)

Try putting the query into a stored procedure and invoking that from your ASP instead of a direct query.

In T-SQL that would be
create procedure MyTest
(
    @intFlatNum   int,
    @daJoiningDate   date,
    @daLeavingDate    date
)
as
Begin
    set nocount on
    SELECT Flat ,First_Name , Middle_Name , Last_Name , Mobile_No , Email, DOB , Age , Education ,  
                  Office_Add , Native_Add , PAN_Card , Aadhar_Card , Religion , Business_Job , 
                  Married_Unmarried ,  No_Of_Members , Joining_Date ,Leaving_Date 
    from     dbo.Ex_Primary_Member 
    where   Flat = @intFlatNum
       and     Joining_Date = @daJoiningDate
       and     Leaving_Date = @daLeavingDate
End

Open in new window


It'll be more secure (look up "SQL Code Injection") and also you'll be able to test it very easily with all sorts of edge cases (e.g. null for all three input parameters!) before you attach it to your front-end code. This should make it much easier for you to see where your problem is.

hth

Mike
0
Jacques Bourgeois (James Burger)PresidentCommented:
It might not be that the data is not displayed, it can simply be that there is no data. First thing is to make sure that you are retrieving something.

Check the DataReader HasRows property is True. If not, then the problem is with your SQL. Try what was suggested by previous experts.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.