Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
C#
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
Updated 22-Jul-14 0:47am
v2
Comments
Raje_ 22-Jul-14 6:45am    
What error you are getting?
Zaina89 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 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

"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...
C#
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.
 
Share this answer
 
v2
Comments
Zaina89 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 22-Jul-14 11:12am    
Take an "m" out: you misspelled it in you original...
Zaina89 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900