locked
Store session username to sql RRS feed

  • Question

  • User1949315409 posted

    Hi

    I've a .cs page with Session["Username"] = Request.ServerVariables["Logon_user"];

    How do I save the username in my db?

    Thx
    Fili

    Friday, June 1, 2012 6:07 AM

Answers

  • User1320101480 posted

    This is how you insert

       string name = Session["Username"].ToString();
    
       string connString = ConfigurationManager.ConnectionStrings["yourconnstringInWebConfig"].ConnectionString;
       SqlConnection conn = null;
       try
       {
              conn = new SqlConnection(connString);
              conn.Open();
    
              using(SqlCommand cmd = new SqlCommand())
              {
                     cmd.Conn = conn;
                     cmd.CommandType = CommandType.Text;
                     cmd.CommandText = "INSERT INTO dummyTable(name) Values (@var)";
                     cmd.Parameters.AddWithValue("@var", name);
                     int rowsAffected = cmd.ExecuteNonQuery();
                     if(rowsAffected ==1)
                     {
                            //Success notification
                     }
                     else
                     {
                            //Error notification
                     }
              }
       }
       catch(Exception ex)
       {
              //Error message
       }
       finally
       {
              if(conn!=null)
              {
                     //Close connection
              }
       }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 1, 2012 6:30 AM

All replies

  • User-1187166364 posted

    Create a DB Connection

    Create a query 

    something like this

    insert into LoogedInUsers (LoogedInUserID) values ('"+Session["Username"].ToString()+"');

    again it depends what way of data access you are using..

    Direct SQL

    SQL with Stored procedure

    LinqtoSQl

    ADO.Entity

    but the logic remains same.

    Friday, June 1, 2012 6:13 AM
  • User52625461 posted

    Hi

    I've a .cs page with Session["Username"] = Request.ServerVariables["Logon_user"];

    How do I save the username in my db?

    Thx
    Fili

    http://www.dreamincode.net/forums/topic/66695-insert-data-into-sql-server-in-vbnet-by-user-input/

    Friday, June 1, 2012 6:22 AM
  • User1320101480 posted

    This is how you insert

       string name = Session["Username"].ToString();
    
       string connString = ConfigurationManager.ConnectionStrings["yourconnstringInWebConfig"].ConnectionString;
       SqlConnection conn = null;
       try
       {
              conn = new SqlConnection(connString);
              conn.Open();
    
              using(SqlCommand cmd = new SqlCommand())
              {
                     cmd.Conn = conn;
                     cmd.CommandType = CommandType.Text;
                     cmd.CommandText = "INSERT INTO dummyTable(name) Values (@var)";
                     cmd.Parameters.AddWithValue("@var", name);
                     int rowsAffected = cmd.ExecuteNonQuery();
                     if(rowsAffected ==1)
                     {
                            //Success notification
                     }
                     else
                     {
                            //Error notification
                     }
              }
       }
       catch(Exception ex)
       {
              //Error message
       }
       finally
       {
              if(conn!=null)
              {
                     //Close connection
              }
       }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 1, 2012 6:30 AM