How do you actually display data that is retrieved from database according to the current login user? I used this method but it is actually not working. Where have i gone wrong?
My .cs code:
int selectedRow = GridView1.SelectedIndex;
GridViewRow row = GridView1.Rows[selectedRow];
int UserId = Convert.ToInt32(row.Cells[0].Text);
MembershipUser currentUser = Membership.GetUser();
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
string strConnectionString = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "SELECT date, starttime, endtime, planner FROM bookings WHERE UserId =@UserId";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();
SqlDataReader reader = cmd.ExecuteReader();
cmd.Parameters.AddWithValue("@UserId", UserId);
reader.Close();
myConnect.Close();
To clarify, you are executing the DataReader BEFORE you add the UserId parameter. In fact, it’s a good idea not even to open the connection until your command object is fully defined.
It should look more like this: