0

My code is working perfectly, it says the row was saved but when I take a look in the database no row was saved. Can you help me please?

I want to save a row using a stored procedures, the checkifusernameexist is working but the save function do not save the data.

Here's my code, hope you can understand it.

using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;

namespace storedprocedures
{
    public partial class Login : System.Web.UI.Page
    {
        public string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["practiceDatabaseConnectionString"].ConnectionString;
        }

        private void executeAdd(string username, string password)
        {
            SqlConnection conn = new SqlConnection(GetConnectionString());
            conn.Open();

            SqlCommand cmd = new SqlCommand("usp_CreateUser", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("userName",userName.Text);
            SqlParameter p2 = new SqlParameter("password",password.ToString());
            cmd.Parameters.Add(p1);
            cmd.Parameters.Add(p2);

            SqlDataReader dReader = cmd.ExecuteReader();

            if (dReader.HasRows)
            {
                dReader.Read();     
                Label3.Text="Record Added!";
            }
            else
            {
                Label3.Text="User Error";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(GetConnectionString());
            con.Open();

            SqlCommand cmd = new SqlCommand("usp_CheckIfUserNameExist",con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("userName", userName.Text);
            cmd.Parameters.Add(p1);
            SqlDataReader dReader = cmd.ExecuteReader();

            if (dReader.HasRows)
            {
                dReader.Read();
                this.Label3.ForeColor = System.Drawing.Color.Red;
                this.Label3.Text = "Username already exist!";
            }
            else
            {
                this.Label3.Text = "congrats! nanalo ka na!";
                this.userName.Text = "";
                this.password.Text = "";
            }
        }
    }
}
1
  • You need to show us your connection string!!
    – marc_s
    Commented Feb 29, 2012 at 6:17

1 Answer 1

3

Use cmd.ExecuteNonQuery() for adding records.

1
  • 2
    The return from cmd.ExecuteNonQuery() is an integer, which you can check for how many rows were modified with the query.
    – Developer
    Commented Feb 29, 2012 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.