Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello programmers, got a problem here as you see i am trying to change vb.net to C#. The problem is that i used developerfusion to convert it. Now ive got a problem on session.
on my code in first page im using vb.net
here is the code
VB
Imports System.Math
Imports System.Web
Imports System.Data.SqlClient
Imports System.Data
Partial Class LOGIN
    Inherits System.Web.UI.Page

    Protected Sub btnLogin_OnClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Dim strSQL As String
        Dim strUserURL As String
        Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Integrated Security=SSPI;" & "Initial Catalog=TestSQL")
        strSQL = "SELECT * FROM [Tried] " _
                    & "WHERE Username='" & Replace(txtUsername.Text, "'", "''") & "' " _
                    & "AND Password='" & Replace(txtPassword.Text, "'", "''") & "'"
        Dim command As New SqlCommand(strSQL, connection)
        connection.Open()
        strUserURL = command.ExecuteScalar()
        connection.Close()
        If strUserURL <> "" Then
            lblInvalid.Text = ""
            FormsAuthentication.SetAuthCookie(txtUsername.Text, True)
            Session("username1") = txtUsername.Text
            Response.Redirect("Chatroom.aspx")
        Else
            lblInvalid.Text = "Sorry... try again..."
        End If


    End Sub
End Class

on my second page Home I used C#
and got this error
MIDL
Timer1.Tick += new EventHandler(Timer1_Tick);

   callerNames = new AsyncCallingNames(this.RefreshListNames);
   callerMessages = new AsyncCallingMessages(this.RefreshMessages);

   Label1.Text = DateTime.Today.ToString("dd-MMM-yyyy");
   string username = Session["username1"];
   lbluser.Text = username;
   logdata();

Compilation Error <br />

Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)<br />

Plss help. Is Session declaration in C# different in Vb.net?
Thanks and more power
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jul-11 1:31am    
In what line of code? Also, this is self-describing. String is object, but object may or may not be string, so what would you expect?
--SA

You need to add a ToString method call. Like this:

C#
string username = Session["username1"].ToString();


Edit: You need to add an object to session in page one. Following should help:

In Page1:

Session.Add("Key","Value");


In Page2:

string sessionValue = Convert.ToString(Session["Key"]);
 
Share this answer
 
v2
Comments
janwel 18-Jul-11 1:32am    
sir how about passing to another page?
dan!sh 18-Jul-11 1:38am    
Updated the reply.
janwel 18-Jul-11 2:03am    
sir on Session.add("key", "Value"); Sir what will i input say I want the textbox1.text be session is this correct session.add("Textbox1.text","text"); sorry sir im new in C
dan!sh 18-Jul-11 3:34am    
Key will be the something that the text represents. I think username in your case. Value will be the value you want to store in the object.
Try using
string username = Convert.ToString(Session["username1"]);<br />
If sessionname is null, this will return String.Empty rather than throw an error.
 
Share this answer
 
v3
use this code when you retrieving user name:

string username = Convert.ToString(Session["username1"]);
if(!string.IsNullOrWhiteSpace(username))
{
      //Do..
}
 
Share this answer
 
v3
Comments
dan!sh 18-Jul-11 1:32am    
Convert.ToString will automatically return empty string if the session value is null.
janwel 18-Jul-11 1:32am    
sir how about passing to another page?
walterhevedeich 18-Jul-11 1:40am    
What do you mean pass to another page? The user name?
janwel 18-Jul-11 1:50am    
yes sir plss help
Abhinav S 18-Jul-11 1:52am    
The user name is already in the session. Just access the same object from another page and it should work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900