The Artima Developer Community
Sponsored Link

C# Answers Forum
insert statement error

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
samir dutta

Posts: 1
Nickname: hisamir
Registered: Sep, 2008

insert statement error Posted: Aug 2, 2009 11:48 PM
Reply to this message Reply
Advertisement
dear all

am getting insert error, confirming my connectionstring is working properly and am working on vs2005

<appSettings>
<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\data.mdb"/>
</appSettings>
===============

using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
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;


public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

private void ExecuteInsert(string Firstname, string Lastname, string Password, string Repassword, string email, string question, string answer, string age, string gender, string mobile, string country, string city, string occupation)

{
OleDbConnection sqlConn = new OleDbConnection(ConfigurationManager.AppSettings["ConnectionString"]);

string strSQL = "INSERT INTO Logindls (Firstname, Lastname, Password, Repassword, email,question, answer, age, gender, mobile, country, city, occupation) VALUES "
+ " (@Firstname,@Lastname,@Password,@Repassword, @email,@question, @answer, @age,@gender, @mobile, @country, @city, @occupation)";
try
{
sqlConn.Open();
OleDbCommand sqlCmd = new OleDbCommand(strSQL, sqlConn);
OleDbParameter[] param = new OleDbParameter[13];

//param[0] = new SqlParameter("@id", SqlDbType.Int, 20);

param[0] = new OleDbParameter("@Firstname", OleDbType.Char, 50);
param[1] = new OleDbParameter("@Lastname", OleDbType.Char, 50);
param[2] = new OleDbParameter("@Password", OleDbType.Char, 8);
param[3] = new OleDbParameter("@Repassword", OleDbType.Char, 8);
param[4] = new OleDbParameter("@email", OleDbType.Char, 50);
param[5] = new OleDbParameter("@question", OleDbType.Char, 50);
param[6] = new OleDbParameter("@answer", OleDbType.Char, 50);
param[7] = new OleDbParameter("@age", OleDbType.Char, 50);
param[8] = new OleDbParameter("@gender", OleDbType.Char, 50);
param[9] = new OleDbParameter("@mobile", OleDbType.Char, 50);
param[10] = new OleDbParameter("@country", OleDbType.Char, 50);
param[11] = new OleDbParameter("@city", OleDbType.Char, 50);
param[12] = new OleDbParameter("@occupation", OleDbType.Char, 50);

param[0].Value = Firstname;
param[1].Value = Lastname;
param[2].Value = Password;
param[3].Value = Repassword;

param[4].Value = email;
param[5].Value = question;
param[6].Value = answer;
param[7].Value = age;
param[8].Value = gender;
param[9].Value = mobile;
param[10].Value = country;
param[11].Value = city;
param[12].Value = occupation;

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

sqlCmd.CommandType = CommandType.Text;
sqlCmd.ExecuteNonQuery();
}
catch (System.Data.OleDb.OleDbException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);

}
finally
{
sqlConn.Close();
}
}

protected void btnRegister_Click(object sender, EventArgs e)
{
{
if (txtUserPwd.Text == txtReUserPwd.Text)
{
//call the method to execute insert to the database
ExecuteInsert(txtFirstName.Text, txtLastName.Text, txtUserPwd.Text, txtReUserPwd.Text, txtEmailid.Text, txtQuestion.Text, txtAnswer.Text, txtAge.Text, ddlGender.SelectedItem.Text, txtMobileNo.Text, ddlCountry.SelectedItem.Text, txtCity.Text, ddlOccupation.SelectedItem.Text);
Response.Write("Record was successfully added!");
ClearControls(Page);
}
else
{
Response.Write("Password did not match");
txtUserPwd.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);
}
}
}

Topic: Add DLL in C#.net Previous Topic   Next Topic Topic: Assembly Registration - C# Beginner

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use