473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ERROR: invalidoperationexception was unhandled by user code

Creating a Simple Registration Form in ASP.NET,

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlTypes;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10.  
  11.  
  12. public partial class Default2 : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.  
  17.     }
  18.     public string GetConnectionString()
  19.     {
  20.  
  21.         //sets the connection string from your web config file "ConnString" is the name of your Connection String
  22.  
  23.         return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
  24.  
  25.     }
  26.     private void ExecuteInsert(string name, string username, string password, string gender, string age, string address)
  27.     {
  28.  
  29.         SqlConnection conn = new SqlConnection(GetConnectionString());
  30.  
  31.  
  32.  
  33.         string sql = "INSERT INTO tblRegistration (Name, UserName, Password, Gender, Age, Address) VALUES "
  34.  
  35.                     + " (@Name,@UserName,@Password,@Gender,@Age,@Address)";
  36.  
  37.         try
  38.         {
  39.  
  40.             conn.Open();
  41.  
  42.             SqlCommand cmd = new SqlCommand(sql, conn);
  43.  
  44.             SqlParameter[] param = new SqlParameter[6];
  45.  
  46.  
  47.  
  48.             param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
  49.  
  50.             param[0] = new SqlParameter("@Name", SqlDbType.VarChar, 50);
  51.  
  52.             param[1] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
  53.  
  54.             param[2] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
  55.  
  56.             param[3] = new SqlParameter("@Gender", SqlDbType.Char, 10);
  57.  
  58.             param[4] = new SqlParameter("@Age", SqlDbType.Int, 100);
  59.  
  60.             param[5] = new SqlParameter("@Address", SqlDbType.VarChar, 50);
  61.  
  62.  
  63.  
  64.             param[0].Value = name;
  65.  
  66.             param[1].Value = username;
  67.  
  68.             param[2].Value = password;
  69.  
  70.             param[3].Value = gender;
  71.  
  72.             param[4].Value = age;
  73.  
  74.             param[5].Value = address;
  75.  
  76.  
  77.  
  78.             for (int i = 0; i < param.Length; i++)
  79.             {
  80.  
  81.                 cmd.Parameters.Add(param[i]);
  82.  
  83.             }
  84.  
  85.  
  86.  
  87.            cmd.CommandType = CommandType.Text;
  88.  
  89.             cmd.ExecuteNonQuery();
  90.  
  91.         }
  92.  
  93.         catch (System.Data.SqlClient.SqlException ex)
  94.         {
  95.  
  96.             string msg = "Insert Error:";
  97.  
  98.             msg += ex.Message;
  99.  
  100.             throw new Exception(msg);
  101.  
  102.  
  103.  
  104.         }
  105.  
  106.         finally
  107.         {
  108.  
  109.             conn.Close();
  110.  
  111.         }
  112.  
  113.     }
  114.  
  115.     protected void Button1_Click(object sender, EventArgs e)
  116.     {
  117.         if (TxtPassword.Text == TxtRePassword.Text)
  118.         {
  119.  
  120.             //call the method to execute insert to the database
  121.  
  122.             ExecuteInsert(TxtName.Text, TxtUserName.Text, TxtPassword.Text, DropDownList1.SelectedItem.Text, TxtAge.Text, TxtAddress.Text);
  123.  
  124.  
  125.  
  126.             Response.Write("Record was successfully added!");
  127.  
  128.  
  129.  
  130.             ClearControls(Page);
  131.  
  132.         }
  133.  
  134.         else
  135.         {
  136.  
  137.             Response.Write("Password did not match");
  138.  
  139.             TxtPassword.Focus();
  140.  
  141.         }
  142.     }
  143.     public static void ClearControls(Control Parent)
  144.     {
  145.  
  146.         if (Parent is TextBox)
  147.  
  148.         { (Parent as TextBox).Text = string.Empty; }
  149.  
  150.         else
  151.         {
  152.  
  153.             foreach (Control c in Parent.Controls)
  154.  
  155.                 ClearControls(c);
  156.  
  157.         }
  158.  
  159.     }
  160. }
  161.  
when debugging my page after entering the details it's showing one error like "invalidoperationexception was unhandled by user code"
Nov 5 '11 #1
1 4708
Maraj
24
Muralikrishna!I think you are inserting records in table which have 6 columns but when you are assigning values to variable param you have passed 7 values and in the table first argument is string and in param[0] you have assigned an int value.You have created
param[6] and you are passing 7 values.
See this if it works.
Nov 6 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: vooose | last post by:
Is there any way to handle exceptions thrown by .NET classses? During runtime and at seemingly random times, exceptions similar to the ones shown at the base of this post are thrown. These errors...
1
by: D A H | last post by:
I have gotten the same exception in multiple projects. I have solved the underlying problem. My question is if anyone knew of a setting that would cause this exception to be thrown. A...
1
by: Tim::.. | last post by:
Hi can someone please explain to me why I get the following error when I place the following code into a server control. It worked fine until I created the control??? Error:...
2
by: Mesut KoÅŸucu | last post by:
hi all i get a error when update user attributes the error is "General access denied error " the code is Dim strAttrName As String = Me.TextBox1.Text Dim strAttrValue As String =...
2
by: RD | last post by:
Public Sub New() MyBase.New Me.Url = "http://localhost/RmtUnix2WinPrtCleanupXmlServ/CleanupUnix2WinPrtFiles.asmx" End Sub Debuggfer stops on Mybase.New witrh following error
0
by: Mark | last post by:
I get the error InvalidOperationException Message: Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'. The request failed with the error message: --...
0
by: Blasting Cap | last post by:
I had some old code set to throw an email to me when a user hit an exception. VS 2003 worked fine with the code, VS 2005 when I run it in debug mode pauses over the code each time, and gives me...
4
by: bg_ie | last post by:
Hi, I have the following Program.cs - namespace TestFrameworkApplication { static class Program { /// <summary> /// The main entry point for the application.
4
by: asmx126453 | last post by:
This is the error i Get Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index It points to this code if (!IsPostBack) ...
20
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.