Click here to Skip to main content
15,564,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am doing a project on doctor patient portal in asp.net (c#) where I have to book appointment and it should shown in grid view as view appointment details. If I book an appointment with a doctor(suppose its name is john)the doctor john should see appointment details of the patient whom book an appointment.

Here my problem is grid view is not displayed.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class viewappointment : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=DESKTOP-9RP88PP;Initial Catalog=Project;Integrated Security=True");
    string uname;
    protected void Page_Load(object sender, EventArgs e)
    {
        uname=Session["email"].ToString();
        con.Open();
        string sql = "select * from appointmt where doctor_name='" + uname + "'";
        SqlDataAdapter adp = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
Posted
Updated 6-Nov-22 22:32pm
v2
Comments
Richard Deeming 7-Nov-22 4:43am    
string sql = "select * from appointmt where doctor_name='" + uname + "'";

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.
using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-9RP88PP;Initial Catalog=Project;Integrated Security=True"))
{
    const string sql = "select * from appointmt where doctor_name = @uname";
    SqlDataAdapter adp = new SqlDataAdapter(sql, con);
    adp.SelectCommand.Parameters.AddWithValue("@uname", uname);
    
    DataSet ds = new DataSet();
    adp.Fill(ds);
    
    GridView1.DataSource = ds;
    GridView1.DataBind();
}

1 solution

We can't help you: we can't run that fragment of code with your database, and your session details - and you need them all to be sure what the problem might be.

At a guess, the "email" in the session is not the same as any row in the "doctor_name" column in your DB - but a guess is all it can be.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
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