Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
protected void btnregister_Click(object sender, EventArgs e)
{

if (Page.IsValid)
{
string cs = ConfigurationManager.ConnectionStrings["SampleConnectionString"].ConnectionString;

using (SqlConnection con = new SqlConnection(cs))
using(SqlCommand cmd = new SqlCommand("spRegisterUser", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", txtusername.Text);
cmd.Parameters.AddWithValue("@Password", txtpassword.Text);
cmd.Parameters.AddWithValue("@Email", txtemail.Text);
con.Open();
int count = (int)cmd.ExecuteScalar();
if ( count == -1)
{
Label1.Text = "User id is not avalilable please choose another one";
}
else
{

Response.Redirect("~/Login.aspx");
}

}


and Stored procedure is

CREATE Procedure spRegisterUser   
@Username nvarchar(50),  
@Password nvarchar(50),  
@Email nvarchar(50)  
As  
Begin  
  Declare @Count int  
  Declare @Returncount int  
    
   Select @Count = COUNT(Username)  
   from tblUsers Where Username=@Username  
    
   if @Count > 0   
   Begin  
   Set @Returncount = -1  
   End  
   Else  
   Begin  
   set @Returncount = 1  
   Insert into tblUsers values(@Username,@Password,@Email)  
   End  
   Select @Returncount as ReturnValue  
   End


i don't know iam not getting any exception and the values not getting stored. please help me is there anything wrong with that code
Posted
Updated 25-Oct-14 20:23pm
v3

Give correct Stored procedure name and set CommandType as StoredProcedure

SqlCommand cmd = new SqlCommand("spRegisterUser", con);
cmd.CommandType = CommandType.StoredProcedure;


Code:

C#
using (SqlConnection con = new SqlConnection(cs))
using(SqlCommand cmd = new SqlCommand("spRegisterUser", con))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Username", txtusername.Text);
    cmd.Parameters.AddWithValue("@Password", txtpassword.Text);
    cmd.Parameters.AddWithValue("@Email", txtemail.Text);
    con.Open();
    int count = Convert.ToInt32(cmd.ExecuteScalar());
}
 
Share this answer
 
v2
Comments
raxhemanth 26-Oct-14 2:07am    
ya i have changed it to stored procedure but not getting
DamithSL 26-Oct-14 2:10am    
have you change the parameter adding code as well?
raxhemanth 26-Oct-14 2:13am    
still getting the same? no warning no exception and nothing not getting saved
raxhemanth 26-Oct-14 2:20am    
ya i changed the code too as you mentioned above
DamithSL 26-Oct-14 2:24am    
have you debug and check whether your application execute this SP or not, and what is the vale you get as count?
I did the requirement.. Try this task..I mean Check stored procedure and logic definitely you will get the result...

http://www.stuffuser.in/2014/04/grid-view-crud-operations-using.html[^]
 
Share this answer
 
v2

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