Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I have created a form that is linked to access base (Apply Table), so what I want is when I fill the form and click the button, the information goes directly to the database, but I still have an error
Could someone check my code and fix it please
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;
using System.Data;
 
namespace QIS
{
    public partial class Applyonline : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void TextBox8_TextChanged(object sender, EventArgs e)
        {
 

        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
                    try
                {
                    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                    String comandString ;
 
                    OleDbConnection con = new OleDbConnection("connStr");
                    OleDbDataAdapter adp = new OleDbDataAdapter();
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.Connection = con;
 
                    adp.SelectCommand = cmd;
                    con.Open();
                    comandString = "INSERT into Apply(FirstName,LastName,Gender,DateOfBirth,CountryOfBirth,Nationality,PrimaryLanguage,NameOfCurrentSchool) values ( " + tb_FirstName.Text + " ," + tb_LastName.Text + " , " + tb_Gender.Text + " , " + tb_Birth.Text + ", " + tb_CountryOfBirth.Text + " , " + tb_Nationality.Text + " , " + tb_Language.Text + ", " + tb_School.Text + ")";
                    
                    cmd.ExecuteNonQuery();
                    lbl_msg.Text = "Thank you for applying, we well get back to you soon.";
                    con.Close();
                }
                catch
                {
                    lbl_msg.Text = "Error Occured.";
                }
            }
 
        }
 
        }
Posted 22-Jul-14 1:04am
Zaina89160
Edited 22-Jul-14 1:47am
Raje_7.4K
v2
Comments
Raje_ at 22-Jul-14 6:45am
   
What error you are getting?
Zaina89 at 22-Jul-14 11:03am
   
Every time I click on the button, it won't save the data into the database , and the second text msg appear (Error Occurred)
Maciej Los at 22-Jul-14 6:51am
   
Do not repost: http://www.codeproject.com/Questions/799482/How-to-insert-data-into-access-database?arn=19

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

"How to insert data into access database"
 
By preference, not like that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
That will actually cure your problem as well...Once you attach the command string to the command object, which you don't at present...
comandString = "INSERT into Apply(FirstName,LastName,Gender,DateOfBirth,CountryOfBirth,Nationality,PrimaryLanguage,NameOfCurrentSchool) values ( " + tb_FirstName.Text + " ," + tb_LastName.Text + " , " + tb_Gender.Text + " , " + tb_Birth.Text + ", " + tb_CountryOfBirth.Text + " , " + tb_Nationality.Text + " , " + tb_Language.Text + ", " + tb_School.Text + ")";
cmd.CommandText = commandString;   // ADD THIS.
cmd.ExecuteNonQuery();
 
Adding that will get rid of your current problem, but it will produce another: you will need to use parametrized queries to get rid of that properly.
  Permalink  
v2
Comments
Zaina89 at 22-Jul-14 11:03am
   
I have tried your solution, but it doesn't work, Every time I click on the button, it won't save the data into the database , and the second text msg appear (Error Occurred)
OriginalGriff at 22-Jul-14 11:12am
   
Take an "m" out: you misspelled it in you original...
Zaina89 at 22-Jul-14 12:17pm
   
Yes I have fixed that, but I still got the same result, Every time I click on the button, it won't save the data into the database , and the second text msg appear (Error Occurred)
OriginalGriff at 22-Jul-14 12:28pm
   
I did say "it will produce another"
 
And also "you will need to use parametrized queries to get rid of that properly"
 
This time the error is coming from Access - and it saying that the command you are passing is bad - and it is - so change to parametrized queries and that one will go away as well!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 290
1 Jochen Arndt 165
2 DamithSL 125
3 PIEBALDconsult 110
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 22 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100