Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am inserting data by asp.net c# in ms access database.
my program run successfully but records not insert in data base
please help...

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
                     OleDbConnection con = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();
            
            con.ConnectionString=@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\manish\Desktop\myprogram\demo.accdb;User ID=admin";
            cmd.Connection = con;
            string sql="insert into data1(name,lastname)values(" + txtfirstname.Text + "," + txtlastname.Text + ")";
            try{

               con.Open();
               cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch(Exception)
            {
                con.Close();

            }
        }
    }
}
Posted
Updated 4-Feb-18 0:55am
v2

Your code is SQL Injection[^] vulnerable.
You should use parameterized queries[^] instead of string-concatenating query! Here is a sample code: OleDbCommand.Parameters Property (System.Data.OleDb)[^]

On the other hand, your connection string seems to be wrong. Please, check this: Access connection strings - ConnectionStrings.com[^]
 
Share this answer
 
1) Do not hard code connection strings: it will always come back to bite you later. Always use a configuration file to store them. That way you can change them as necessary when you release code to other people.

2) Always treat connection, command and suchlike objects as scarce resources and Dispose of them when you are finished with them. A using block is the simplest way to do this as it automatically calls Dispose whenteh object goes out of scope.

3) Never 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.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

4) Never swallow exceptions - it means that the essential data you need to fix the problem is thrown away - that means never using this:
C#
catch(Exception)
because you have no access to what the exception was, and the message which tells you why the system failed. Instead, use this:
C#
catch(Excpetion ex)
and use logging or even a MessageBox to display the error message the Exception object contains. That way, you get to find out what the problem is, and can fix it...
 
Share this answer
 

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