Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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::::
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 26-Apr-13 0:09am
Edited 26-Apr-13 0:16am
(no name)47.8K
v3
Comments
kkakadiya at 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...
Member 10009437 at 26-Apr-13 9:28am
   
Upid is user profile id in my database which is set to auto increment.
ryanb31 at 26-Apr-13 7:42am
   
Need more info. What line is causing this? You have 2 sql statements, which one is causing the error?
Member 10009437 at 26-Apr-13 9:27am
   
updatedata query causing error. In my database Upid is user profile id which is auto increment.
Member 10009437 at 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.
ryanb31 at 26-Apr-13 9:33am
   
p.Upid is probably null. Put a breakpoint and walk through and see what is happening.

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 26 Apr 2013
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