user5695111
user5695111

Reputation: 109

asp.net Conversion failed when converting the nvarchar value to data type int using session

I want to fill the GridView reading Session variable Id but I got error that to converting data type as like {"Conversion failed when converting the nvarchar value 'finalProject.User' to data type int."} but I haven't nvarchar value in my Database table it's already int. How to I fix it?

finalProject.User

public class User
{
    public int Id { get; set; }
    public string name { get; set; }
    public string surname { get; set; }
    public string email { get; set; }
}

User object

User u = new User();
                u.Id = Convert.ToInt32(dr["Id"]);
                u.name = dr["name"] != DBNull.Value ? dr["name"].ToString() : string.Empty;
                u.surname = dr["surname"] != DBNull.Value ? dr["surname"].ToString() : string.Empty;
                u.email = dr["email"] != DBNull.Value ? dr["email"].ToString() : string.Empty;
                Session["user"] = u;

My codes

public partial class MyConferences : System.Web.UI.Page
{
    internal User user;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindGrid();
        }
    }

    private void BindGrid()
    {
        object user = Session["user"];

        if(user != null)
        {
            user = Session["user"] as User;
        }           

        string constr = ConfigurationManager.ConnectionStrings[0].ConnectionString;

        using(SqlConnection con = new SqlConnection(constr))
        {
            string query = "select Conferences.conferenceName , Conferences.conferenceDate , Conferences.conferencePlace , Conferences.category from Conferences inner join Users on Conferences.fk_Users = Users.Id where Users.Id = @UserId";

            using(SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Connection = con;
                Convert.ToInt32(Session["UserId"]);

                SqlParameter prm = cmd.Parameters.AddWithValue("@UserId", user.ToString());

                if(user == null)
                {
                    prm.Value = DBNull.Value;
                }

                con.Open();
                GridView1.DataSource = cmd.ExecuteReader();
                GridView1.DataBind();
                con.Close();
            }
        }        
    }
}

Error enter image description here

Upvotes: 0

Views: 1134

Answers (3)

Rajeesh Madambat
Rajeesh Madambat

Reputation: 3751

I presume you are passing a wrong value "user.ToString()" rather "Session["UserId"]". The user is an object and userId looks like a single id value. Dont forget to convert before sending value.

Upvotes: 1

Usman
Usman

Reputation: 4693

mark_h is right you are passing string value to int but you also dont know how to pass the id. It would be better if you give proper type instead of object.

object user = Session["user"] as User;

will be

User user = Session["user"] as User;

and

SqlParameter prm = cmd.Parameters.AddWithValue("@UserId", user.Id);

Upvotes: 3

mark_h
mark_h

Reputation: 5477

It appears you are trying to pass a string as an integer;

            SqlParameter prm = cmd.Parameters.AddWithValue("@UserId", user.ToString());

UserId is an integer but user.ToString() is a string. Did you mean to use this as your parameter instead?

Convert.ToInt32(Session["UserId"])

Upvotes: 1

Related Questions