Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8436499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:11:38+00:00 2026-06-10T07:11:38+00:00

The problem occuring on updating only email all other blanks get null . Even

  • 0

The problem occuring on updating only email all other blanks get null . Even if i unchecked allow null in sql server 2008 .my code is-

protected void Updateinfo_Click(object sender, EventArgs e)
{
    string radiogender;
    if (Radiochngmale.Checked == true)
        radiogender = Radiochngmale.Text.ToString();
    else
        radiogender = Radiochngfemale.Text.ToString();
    SqlConnection con = new SqlConnection();
    con.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
    con.Open();
    if (con.State == ConnectionState.Open)
    {
        SqlCommand cmd = new SqlCommand();
        Random r = new Random();
        int next = r.Next();
        if (FileUpload2.HasFile)
        {
            string myMap = MapPath("~/").ToLower();
            string ImageName = FileUpload2.PostedFile.FileName;
            sImageFileExtension = ImageName.Substring(ImageName.LastIndexOf(".")).ToLower();
            if (sImageFileExtension == ".gif" || sImageFileExtension == ".png" || sImageFileExtension == ".jpg" || sImageFileExtension == ".jpeg" || sImageFileExtension == ".bmp")
            {
                string ImageSaveURL = myMap + "UserImage/" + next + sImageFileExtension;
                FileUpload2.PostedFile.SaveAs(ImageSaveURL);
            }
            else
                Response.Write("Invalid File");
        }
        cmd.Connection = con; 
        if(chngfname.Text==null)
            chngfname.Text="Select Firstname from Login where Email='"+Session["UserName"]+"'";
        if (chnglastname.Text == null)
            chnglastname.Text = "Select Lastname from Login where Email='" + Session["UserName"] + "'";
        if (chngage.Text == null)
            chngage.Text = "Select age from Login where Email='" + Session["UserName"] + "'";
        if (chngemail.Text == null)
            chngemail.Text = "Select Email from Login where Email='" + Session["UserName"] + "'";
        if (radiogender == null)
            radiogender = "Select gender from Login where Email='" + Session["UserName"] + "'";
        if (chngpassword.Text == null)
            chngpassword.Text = "Select Password from Login where Email='" + Session["UserName"] + "'";
        if ( FileUpload2.HasFile==null)
             sImageFileExtension = "Select profile_pic from Login where Email='" + Session["UserName"] + "'";
        if (chngfname.Text == null)
            chngfname.Text = "Select Firstname from Login where Email='" + Session["UserName"] + "'";
        cmd.CommandText =  "Update Login set FirstName = '"+chngfname.Text+"',LastName='"+chnglastname.Text+"',Email='"+chngemail.Text+"',Password='"+chngpassword.Text+"' ,gender='"+radiogender+"',age='"+chngage.Text+"' , profile_pic='"+ next + sImageFileExtension + "' where Email='"+Session["UserName"]+"'";  
        cmd.CommandType = CommandType.Text; 
        cmd.ExecuteNonQuery();
        }
    }

Why didn’t it is taking the previous values even if i mentioned it to take.Please check it out and sort it out

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:11:40+00:00Added an answer on June 10, 2026 at 7:11 am

    This is happening because TextBox.Text is never null so your SQL query ends up looking like this:

    Update Login 
      set FirstName = '', 
          LastName = '',
    where Email = 'John.doe@nowhere.net'
    
    -- etc...
    

    Except for the one or two fields where the data is actually set to something. Here’s probably what you wanted it to look like:

    update login
      set FirstName = 'John', 
          LastName = (select Lastname from login where email = 'John.doe@nowhere.net'), 
          etc...
    where email = 'John.doe@nowhere.net'
    

    But, there no need for the subqueries. If you want to avoid overwriting values where a value is null or empty string, then you want your SQL to look like the following, use Parameters and set them to DbNull when the textbox is empty.

    cmd.Parameters.AddWithValue("@FirstName", (chngfname.Text == String.Empty) ? DbNull.Value : chngfname.Text;
    
    update login
      set FirstName = coalesce(@firstName, FirstName), 
          LastName = coalesce(@LastName, LastName), 
          etc...
    where Email = @Email
    

    The other option is select the record first (which I’m sure you’ve already done) and simply use the same value that’s already in the database.

    if (chngfname.Text == String.Empty) chngfname.Text = Session["CurrentUserEntity"].FirstName;
    

    Additionally, you need to change this to a parametrized query:

    string sql = "update login set FirstName = @firstName, LastName = @lastName, etc... where email = @email;
    cmd.Parameters.Add(...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: Been struggling to get my code to load external shaders and it is
I have problem occuring in my application.. Using XML file i am getting geopoints
If we look at a specific page the problem is occuring: http://www.completeofficechairs.co.uk/RH%20Extend%20220 Where there
I've just started to use linq to sql and have run into a problem
We have a problem occuring on some of our developer workstations: when visiting a
From my research it seems as if this is a re-occuring problem. I'm using
A strange problem occuring in this snippet: class FooController { def barService def update
I have a fairly infrequent problem occuring with source control. In the example here
I'm not certain when this problem started occuring, but it was approximately a few
Current System: Windows XP / Windows 7 (problem occuring for both) After following the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.