Display record based on logged in user using C# in ASP.Net

RichardSa
 
on Aug 27, 2021 11:25 PM
625 Views

I have 2 web forms where data is passed from one to the other, using Session. When data is inserted into database table, user is redirected to the second page and the inserted data is displayed on the second page. But the problem is, if 2 different users insert the same data into the database and click on the insert button, the displayed record is that of the first user.

For example: if user1 and user2 are from two organizations and the names are Freeman Ltd and Rico Investment. User1 has an existing column with the data – “Workers Union Meeting “. Then user2 inserts the same “Workers Union Meeting” from a field into database, and is redirected to the next page, the records that will display is that of user1. But it should be the record of user2 to display.

I want it that even if user2 has a column containing the same data as user1, the displayed record should be that of user2.

I tried this and it did not work. Please help me out here. Thank you

1st page

protected void proceedbtn_Click(object sender, EventArgs e)
{
    string meeting = meetingtxt.Text;
    SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;");
    try
    {
        string sqlStatement = "INSERT INTO Table (Name,Meeting_Name) VALUES (@Name,@Meeting_Name)";
        con.Open();
        SqlCommand cmd = new SqlCommand(sqlStatement, con);
        cmd.Parameters.AddWithValue("@Name", userlabel.Text.ToString());
        cmd.Parameters.AddWithValue("@Meeting_Name", meetingtxt.Text.ToString());
        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }
    catch (SqlException ex)
    {
        string msg = "Insert Error:";
        msg += ex.Message;
        throw new Exception(msg);
    }
    finally
    {
        con.Close();
    }
    Session["meeting"] = meeting;
    Response.Redirect("summary.aspx");
}

SUMMARY PAGE

private void DisplayData()
{
    SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;");
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "SELECT * FROM Table WHERE (Id = '" + Session["user"] + "' AND Meeting_Name = '" + Session["meeting"] + "')";
    cmd.Connection = con;
    SqlDataAdapter sda = new SqlDataAdapter();
    DataSet ds = new DataSet();
    sda.SelectCommand = cmd;
    sda.Fill(ds, "detail");
    if (ds.Tables[0].Rows.Count > 0)
    {
        lblName.Text = ds.Tables[0].Rows[0][1].ToString();
        meetinglbl.Text = ds.Tables[0].Rows[0][2].ToString();
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 28, 2021 05:31 AM

Hi RichardSa,

Refer below code.

1st Page

protected void proceedbtn_Click(object sender, EventArgs e)
{
    string meeting = meetingtxt.Text;
    SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;");
    try
    {
        string sqlStatement = "INSERT INTO Table (Name,Meeting_Name) VALUES (@Name,@Meeting_Name)";
        SqlCommand cmd = new SqlCommand(sqlStatement, con);
        cmd.Parameters.AddWithValue("@Name", userlabel.Text);
        cmd.Parameters.AddWithValue("@Meeting_Name", meeting);
        cmd.CommandType = CommandType.Text;
        con.Open();
        cmd.ExecuteNonQuery();
    }
    catch (SqlException ex)
    {
        string msg = "Insert Error:";
        msg += ex.Message;
        throw new Exception(msg);
    }
    finally
    {
        con.Close();
    }
    Session["user"] = userlabel.Text;
    Session["meeting"] = meeting;
    Response.Redirect("summary.aspx");
}

Summary Page

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Session["user"] != null && Session["meeting"] != null)
        {
            DisplayData();
        }
    }
}

private void DisplayData()
{
    SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;");
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "SELECT * FROM Table WHERE Id = '" + Session["user"].ToString() + "' AND Meeting_Name = '" + Session["meeting"].ToString() + "'";
    cmd.Connection = con;
    SqlDataAdapter sda = new SqlDataAdapter();
    DataTable dt = new DataTable();
    sda.SelectCommand = cmd;
    sda.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        lblName.Text = dt.Rows[0][1].ToString();
        meetinglbl.Text = dt.Rows[0][2].ToString();
    }
}