Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I try to save my edited profile profile data is not save in database and error is come.
error---->>>Must declare the scalar variable "@Upid".
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@Upid".

Source Error: 
Line 75:         con.Open();
Line 76:         cmd.Connection = con;
Line 77:         cmd.ExecuteNonQuery();
Line 78:         con.Close();
Line 79:     }

Source File: c:\Users\swd\Desktop\final project asp.net\App_Code\Logindata.cs    Line: 77

My Datalayer code is::::
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class Logindata
{
    SqlCommand cmd;
    SqlConnection con;
    SqlDataAdapter da;
    DataSet ds;

	public Logindata()
	{
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
	}

    public void insertdata(Loginpropertylayer p)
    {
        cmd = new SqlCommand();
        cmd.CommandText = "add_registration";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Upid", p.Upid);
        cmd.Parameters.AddWithValue("@firstname", p.firstname);
        cmd.Parameters.AddWithValue("@lastname", p.lastname);
        cmd.Parameters.AddWithValue("@address", p.address);
        cmd.Parameters.AddWithValue("@birthdate", p.birthdate); 
        cmd.Parameters.AddWithValue("@state", p.state);
        cmd.Parameters.AddWithValue("@city", p.city);

        cmd.Parameters.AddWithValue("@Gender", p.Gender);
        cmd.Parameters.AddWithValue("@Education", p.Education);
        cmd.Parameters.AddWithValue("@mobilenumber", p.mobilenumber);
        cmd.Parameters.AddWithValue("@Pic", p.Pic);
        cmd.Parameters.AddWithValue("@Email", p.Email);
        cmd.Parameters.AddWithValue("@username", p.username);
        cmd.Parameters.AddWithValue("@password", p.password);
        cmd.Parameters.AddWithValue("@cpassword", p.cpassword);

        con.Open();
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        con.Close();
    }

    public void updatedata(Loginpropertylayer p)
    {
        cmd = new SqlCommand("update tbl_userprofile set firstname=@firstname,lastname=@lastname,address=@address,birthdate=@birthdate,state=@state,city=@city,Gender=@Gender,Education=@Education,mobilenumber=@mobilenumber,Pic=@pic,Email=@Email,uername=@username,password=@password where Upid=@Upid",con);
        cmd.Parameters.AddWithValue("@Upid", p.Upid);
        cmd.Parameters.AddWithValue("@firstname", p.firstname);
        cmd.Parameters.AddWithValue("@lastname", p.lastname);
        cmd.Parameters.AddWithValue("@address", p.address);
        cmd.Parameters.AddWithValue("@birthdate", p.birthdate);
        cmd.Parameters.AddWithValue("@state", p.state);
        cmd.Parameters.AddWithValue("@city", p.city);

        cmd.Parameters.AddWithValue("@Gender", p.Gender);
        cmd.Parameters.AddWithValue("@Education", p.Education);
        cmd.Parameters.AddWithValue("@mobilenumber",p.mobilenumber);
        cmd.Parameters.AddWithValue("@Pic",p.Pic);
        cmd.Parameters.AddWithValue("@Email", p.Email);
        cmd.Parameters.AddWithValue("@username",p.username);
        cmd.Parameters.AddWithValue("@password",p.password);
        //cmd.Parameters.AddWithValue("@cpassword", p.cpassword);

        con.Open();
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        con.Close();
    }


    public DataSet getdata(string uname)
    {
        da = new SqlDataAdapter("select * from tbl_userprofile where username='"+uname+"'", con);
        ds = new DataSet();
        da.Fill(ds);
        return ds;
    }

    public void clear()
    { 
       
    }
}
Posted
Updated 25-Apr-13 23:16pm
v3
Comments
kk2014 26-Apr-13 7:07am    
hi,
please check in database your primary key id.
is it Upid or something else?
your error suggest that it is some naming problem...
Patel Shweta 26-Apr-13 9:28am    
Upid is user profile id in my database which is set to auto increment.
ZurdoDev 26-Apr-13 7:42am    
Need more info. What line is causing this? You have 2 sql statements, which one is causing the error?
Patel Shweta 26-Apr-13 9:27am    
updatedata query causing error. In my database Upid is user profile id which is auto increment.
Patel Shweta 26-Apr-13 9:31am    
from above coding i can easily edit my profile but when i click on second button (save) to save my updated data in database it gives this error. may be my 2 button click event code is wrong.

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



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