Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using MySql.Data.MySqlClient;
 

public partial class profile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["user"].ToString();
    }
 
    protected void Button2_Click(object sender, EventArgs e)
    {
        MySqlConnection con = new MySqlConnection();
        string str = ConfigurationManager.ConnectionStrings["netConnectionString"].ConnectionString;
        con.ConnectionString = str;
        con.Open();
 
        if (con.State.ToString() == "Open")
        {
            MySqlCommand cmd = new MySqlCommand("update users Set education=@c,about=@d,interest=@e where user_name="Session["user"].ToString"",con);
            cmd.Parameters.AddWithValue("@c", TextBox1.Text);
            cmd.Parameters.AddWithValue("@d", TextBox2.Text);
            cmd.Parameters.AddWithValue("@e", TextBox3.Text);
            cmd.ExecuteNonQuery();
            Label5.Text = "Well done profile updated!!";
        }
    }
}
 
I want to update user information from its profile i am getting error at this (where user_name="Session["user"].ToString") see above code please help me
Posted 14-Aug-14 21:48pm
Edited 14-Aug-14 22:08pm
Kim Togo26.2K
v2
Comments
Kim Togo at 15-Aug-14 3:09am
   
Are you getting a compiler error or a runtime error ? - Like an excpetion.
Member 11002578 at 15-Aug-14 3:11am
   
its showing error at ---where user_name="Session["user"].ToString""
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Rewrite your new MySqlCommand to this:
MySqlCommand cmd = new MySqlCommand("UPDATE users SET education=@c,about=@d,interest=@e WHERE user_name=@userName", con);
            cmd.Parameters.AddWithValue("@c", TextBox1.Text);
            cmd.Parameters.AddWithValue("@d", TextBox2.Text);
            cmd.Parameters.AddWithValue("@e", TextBox3.Text);
            cmd.Parameters.AddWithValue("@userName", Session["user"].ToString());
            cmd.ExecuteNonQuery();
            Label5.Text = "Well done profile updated!!";
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You should not give the session value like that. Use it this way:
 
user_name='"+Session.Contents["user"]+"'
 
in the sql query.
Happy Coding Smile | :)
  Permalink  

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

  Print Answers RSS
0 DamithSL 390
1 Maciej Los 217
2 OriginalGriff 213
3 BillWoodruff 135
4 Garth J Lancaster 90
0 OriginalGriff 7,953
1 DamithSL 6,139
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,293
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 15 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100