Creating a Simple Registration Form in ASP.NET, -
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Data.SqlTypes;
-
using System.Data;
-
using System.Data.SqlClient;
-
-
-
public partial class Default2 : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
-
}
-
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["MyConsString"].ConnectionString;
-
-
}
-
private void ExecuteInsert(string name, string username, string password, string gender, string age, string address)
-
{
-
-
SqlConnection conn = new SqlConnection(GetConnectionString());
-
-
-
-
string sql = "INSERT INTO tblRegistration (Name, UserName, Password, Gender, Age, Address) VALUES "
-
-
+ " (@Name,@UserName,@Password,@Gender,@Age,@Address)";
-
-
try
-
{
-
-
conn.Open();
-
-
SqlCommand cmd = new SqlCommand(sql, conn);
-
-
SqlParameter[] param = new SqlParameter[6];
-
-
-
-
param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
-
-
param[0] = new SqlParameter("@Name", SqlDbType.VarChar, 50);
-
-
param[1] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
-
-
param[2] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
-
-
param[3] = new SqlParameter("@Gender", SqlDbType.Char, 10);
-
-
param[4] = new SqlParameter("@Age", SqlDbType.Int, 100);
-
-
param[5] = new SqlParameter("@Address", SqlDbType.VarChar, 50);
-
-
-
-
param[0].Value = name;
-
-
param[1].Value = username;
-
-
param[2].Value = password;
-
-
param[3].Value = gender;
-
-
param[4].Value = age;
-
-
param[5].Value = address;
-
-
-
-
for (int i = 0; i < param.Length; i++)
-
{
-
-
cmd.Parameters.Add(param[i]);
-
-
}
-
-
-
-
cmd.CommandType = CommandType.Text;
-
-
cmd.ExecuteNonQuery();
-
-
}
-
-
catch (System.Data.SqlClient.SqlException ex)
-
{
-
-
string msg = "Insert Error:";
-
-
msg += ex.Message;
-
-
throw new Exception(msg);
-
-
-
-
}
-
-
finally
-
{
-
-
conn.Close();
-
-
}
-
-
}
-
-
protected void Button1_Click(object sender, EventArgs e)
-
{
-
if (TxtPassword.Text == TxtRePassword.Text)
-
{
-
-
//call the method to execute insert to the database
-
-
ExecuteInsert(TxtName.Text, TxtUserName.Text, TxtPassword.Text, DropDownList1.SelectedItem.Text, TxtAge.Text, TxtAddress.Text);
-
-
-
-
Response.Write("Record was successfully added!");
-
-
-
-
ClearControls(Page);
-
-
}
-
-
else
-
{
-
-
Response.Write("Password did not match");
-
-
TxtPassword.Focus();
-
-
}
-
}
-
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);
-
-
}
-
-
}
-
}
-
when debugging my page after entering the details it's showing one error like "invalidoperationexception was unhandled by user code" 1 4708
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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:...
|
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 =...
|
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
|
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:
--...
|
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...
|
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.
|
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)
...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |