Click here to Skip to main content
15,913,587 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class NewRegistration : System.Web.UI.Page
{

    public string GetConnectionString()
    {
        //sets the connection string from your web config file "ConnString" is the name of your Connection String
        return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    }
    

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void ExecuteInsert(string name, string Lastname, string username, string password, string Repassword, string gender, string address,  string Email)
    {

        string sql = "INSERT INTO dance.Registration (Name, Lastname, Username, Password, Repassword, Gender,Address,Email) VALUES "   + " (@Name,@Lastname,@Username,@Password,@Repassword,@Gender,@Address,@Email)";

        try
        {
            SqlConnection conn = new SqlConnection(GetConnectionString());

            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlParameter[] param = new SqlParameter[7];
            //param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
            param[0] = new SqlParameter("@Name", SqlDbType.NVarChar, 50);
            param[1] = new SqlParameter("@Lastname", SqlDbType.NVarChar, 50);
            param[2] = new SqlParameter("@Username", SqlDbType.NVarChar, 50);
            param[3] = new SqlParameter("@Password", SqlDbType.NVarChar, 50);
            param[4] = new SqlParameter("@Repassword", SqlDbType.NVarChar, 50);
            param[5] = new SqlParameter("@Gender", SqlDbType.NChar, 10);
            param[6] = new SqlParameter("@Address", SqlDbType.NVarChar, 100);
            param[7] = new SqlParameter("@Email", SqlDbType.NVarChar, 50);

            param[0].Value = name;
            param[1].Value = Lastname;
            param[2].Value = username;
            param[3].Value = password;
            param[4].Value = Repassword;
            param[5].Value = gender;
            param[6].Value = address;
            param[7].Value = Email;

            for (int i = 0; i < param.Length; i++)
            {
                cmd.Parameters.Add(param[i]);
            }

            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        //finally
        //{
        //    Conn.close(); 
        //}
    }

    public static void ClearControls(Control Parent)
    {

        if (Parent is TextBox)
        { (Parent as TextBox).Text = string.Empty; }
        else
        {
            foreach (Control c in Parent.Controls)
                ClearControls(c);
        }
    }
    protected void Createusrbtn_Click(object sender, ImageClickEventArgs e)
    {
        if (TxtPassword.Text == TxtRePassword.Text)
        {
            //call the method to execute insert to the database
            ExecuteInsert(TxtName.Text,
                          txtLastName.Text,
                          TxtUserName.Text,
                          TxtPassword.Text,
                          TxtRePassword.Text,
                          DropDownList1.SelectedItem.Value,
                          TxtAddress.Text,
                          txtEmailID.Text);

            
            Response.Write("Record was successfully added!");
            ClearControls(Page);
        }
        else
        {
            Response.Write("Password did not match");
            TxtPassword.Focus();
        }

    }
}
Posted
Updated 27-Mar-13 1:51am
v2
Comments
ZurdoDev 27-Mar-13 7:52am    
Would you mind sharing which line of code caused the error?
bbirajdar 27-Mar-13 7:57am    
NAQ
[no name] 27-Mar-13 7:57am    
Additionally, if you used cmd.Parameters.AddWithValue you would not have had this problem to begin with and you have had to write less code.

1 solution

C#
SqlParameter[] param = new SqlParameter[7];


You assign your param variable with a length of 7, but after you try to put 8 values in the array.

You should write

SqlParameter[] param = new SqlParameter[8];


PS: next time, try at least :
- to format your code
- to tell on which row you get the error
- to ask a question
 
Share this answer
 
v2
Comments
Master Vinu 27-Mar-13 7:55am    
thx brother...
phil.o 27-Mar-13 7:57am    
Yes, that's exactly what I wrote. What is your problem ?
[EDIT] This was a reply to navin ks, who has deleted his comment. Not a reply to vinayak.[/EDIT]
navin ks 27-Mar-13 7:57am    
SqlParameter[] param = new SqlParameter[8];
Master Vinu 27-Mar-13 8:01am    
ya problem solved thx

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