0
Answer

need code for update button using stored procedure in hidden id

Ask a question
hari haran

hari haran

14y
8.1k
1
hi 
i create stored procedure like below

ALTER procedure [dbo].[sampleregis]
@RegId int,
@FullName varchar(50),
@UserName varchar(50),
@Password varchar(50),
@RePassword varchar(50),
@Gender char(10),
@Age int,
@Address varchar(50),
@option varchar(10)
as
set nocount off

IF  @option='delete'
 
delete from samplereg1 where RegId=@RegId  

if @option='insert'

IF EXISTS(select FullName,UserName,Password,RePassword,Gender,Age,Address from samplereg1 where FullName=@FullName)
return -1
ELSE
insert into samplereg1 (FullName,UserName,Password,RePassword,Gender,Age,Address)values(@FullName,@UserName,@Password,@RePassword,@Gender,@Age,@Address) 
 
if @option='update'

begin
UPDATE samplereg1 set Fullname=@Fullname,UserName=@UserName,Password=@Password,RePassword=@RePassword,Gender=@Gender,Age=@Age,Address=@Address where RegId=@RegId 
End 


in that i want to make update using button.so i have to use regid as a hidden field.
in my update button code is like below


 TextBox txtRef1=FindControl("TxtName") as TextBox;
            TextBox txtVal2=FindControl("TxtUserName") as TextBox;
            TextBox txtRec3=FindControl("TxtPassword") as TextBox;
            TextBox txtRbv4=FindControl("TxtRePassword") as TextBox;
            TextBox txtRnh5=FindControl("DropDownList1") as TextBox;
            TextBox txtRds6=FindControl("TxtAge") as TextBox;
            TextBox txtRgr7=FindControl("TxtAddress") as TextBox;
            HiddenField hdnRef8=FindControl("hdnRefId") as HiddenField;
            SqlConnection conn = new SqlConnection(GetConnectionString());
            SqlCommand cmd = new SqlCommand("sampleregis", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@RegId", SqlDbType.Int).Value = Convert.ToInt32(hdnRef8.Value); 
            cmd.Parameters.Add("@FullName", SqlDbType.VarChar, 50).Value = txtRef1.Text;
            cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = txtVal2.Text;
            cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = txtRec3.Text;
            cmd.Parameters.Add("@RePassword", SqlDbType.VarChar, 50).Value = txtRbv4.Text;
            cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 50).Value = txtRnh5.Text;
            cmd.Parameters.Add("@Age", SqlDbType.Int).Value = Convert.ToInt32(txtRds6.Text);
            cmd.Parameters.Add("@Address", SqlDbType.VarChar, 50).Value = txtRgr7.Text;
            cmd.Parameters.Add("@option", SqlDbType.VarChar, 10).Value = "update";
            conn.Open();
            
            
            cmd.ExecuteNonQuery();
            conn.Close();
 but if i execuite means ill get error like this line 

cmd.Parameters.Add("@RegId", SqlDbType.Int).Value = Convert.ToInt32(hdnRef8.Value); 


error is 
Object reference not set to an instance of an object.
 so please any one help me .i need urgently.

 thanks in advance
HARI