Chinchu Sara
Chinchu Sara

Reputation: 75

How to Use session in asp.net

hiii..am doing a survey project. i have a text box and two ajax calendar extender. i want to use Survey name as session. How to do that?? am not familiar with session. can anyone help me please???

protected void Button1_Click(object sender, EventArgs e)
    {
        string strcon;
        strcon = ConfigurationManager.ConnectionStrings["SurveyConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strcon);
        SqlCommand com2 = new SqlCommand(strcon);
        com2.Connection = con;
        con.Open();
        com2.CommandText = "insert into SurveyMaster1(SurveyName,DateCreated,ExpiredDate) VALUES(@SurveyName,@DateCreated,@ExpiredDate)";
        com2.Parameters.AddWithValue("@SurveyName", txtSurveyName.Text);
        com2.Parameters.AddWithValue("@DateCreated", txtCreaDate.Text);
        com2.Parameters.AddWithValue("@ExpiredDate", txtExDate.Text);
        com2.ExecuteNonQuery();
        Response.Write("Inserted Successfully");
    con.Close();
    Response.Redirect("Questions.aspx");
}
}

aspx code:

   <asp:Panel ID="Panel1" runat="server">
 <asp:Label ID="Label1" runat="server" ></asp:Label>
    <asp:Label ID="Label2" runat="server" Text="Survey Name"></asp:Label>
    <asp:Label ID="Label4" runat="server" Text="Expired Date"></asp:Label>
    <asp:TextBox ID="txtSurveyName" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtCreaDate" runat="server" ></asp:TextBox>

    <asp:CalendarExtender ID="txtCreaDate_CalendarExtender" runat="server" 
        Enabled="True" TargetControlID="txtCreaDate">
    </asp:CalendarExtender>

    <asp:TextBox ID="txtExDate" runat="server"></asp:TextBox>

    <asp:CalendarExtender ID="txtExDate_CalendarExtender" runat="server" 
        Enabled="True" TargetControlID="txtExDate">
    </asp:CalendarExtender>

    <asp:Button ID="Button1" runat="server" Text="SAVE" onclick="Button1_Click" />

    <asp:RequiredFieldValidator ID="reqFieldSurvey" runat="server" 
        ControlToValidate="txtSurveyName" ErrorMessage="* Enter Survey Name" 
        style="z-index: 1; left: 458px; top: 99px; position: absolute; bottom: 286px"></asp:RequiredFieldValidator>
    <asp:RequiredFieldValidator ID="reqCreDate" runat="server" 
        ControlToValidate="txtCreaDate" ErrorMessage="* Select Date" >   </asp:RequiredFieldValidator>
    <asp:RequiredFieldValidator ID="reqExpDate" runat="server" 
        ControlToValidate="txtExDate" ErrorMessage="* Select Date"></asp:RequiredFieldValidator>

</asp:Panel>

Upvotes: 1

Views: 3194

Answers (5)

Sachin
Sachin

Reputation: 40980

i want to use Survey name as session

If you want to create a session with key as Survey Name then you do this

string surveyName = txtsurvey.Text;
Session["SurveyName"] = surveyName;  // or whatever value, you want to assign in session object.

Upvotes: 1

Subhash Jakhar
Subhash Jakhar

Reputation: 172

You can store the Survey name in Session Name in this way : -

Session["SurveyName"] = txtSurveyName.Text;

and when you want to use Survey name you can get in this way :-

string StrSurveyName = Session["SurveyName"].ToString();

Hope this will Help You

Upvotes: 0

sanjeet
sanjeet

Reputation: 1508

add this code in your .cs file it store your textbox value in session and for your information session stores any type of value because it is of type 'object'

Session["SurveyName"] = TextBox.text;

Hope this will help you!!!!

Upvotes: 3

Adrian Wragg
Adrian Wragg

Reputation: 7411

Your first step if you want to gain more familiarity with the Session object should probably be to read up on the MSDN documentation, at http://msdn.microsoft.com/en-us/library/ms524319.aspx.

To use the session in your code, simply assign to it, thus:

    con.Close();
    Session["SurveyName"] = txtSurveyName.Text;
    Response.Redirect("Questions.aspx");

You are then able to use Session["SurveyName"] in later pages.

Upvotes: 0

Ben Gulapa
Ben Gulapa

Reputation: 1619

Set: Session["SurveyName"] = "A survey name";

Get: txtSurveyName.Text = Session["SurveyName"].ToString();

Upvotes: 3

Related Questions