Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying to execute this sqlite database in C# visual studio windows application but whenever I try to enter value in text boxes this error occur if anyone have idea please help I am using sqlite studio.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
namespace login_data
{
    public partial class Form1 : Form
    {
        string connectionString;
        public Form1()
        {
            InitializeComponent();
            connectionString = @"Data Source=D:\tirth doshi\codes\login data\Login.db;Version=3;New=True;Compress=True;";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (SQLiteConnection sqlite_conn = new SQLiteConnection(connectionString))
            {
                sqlite_conn.Open();
                //     string connect= "INSERT Login WHERE textBox1= @UserName AND textBox2= @PassWord";
                SQLiteCommand cmd = new SQLiteCommand();

                cmd.CommandText = "insert into Login (UserName, PassWord,) values ('" + textBox1.Text + "','" + textBox2.Text + "');";
                cmd.Connection = sqlite_conn;
                cmd.Parameters.AddWithValue("@UserName", textBox1.Text);
                cmd.Parameters.AddWithValue("@PassWord", textBox2.Text);
                cmd.ExecuteNonQuery();
            }
        }
    }   
}
Posted
Updated 25-Feb-21 19:54pm
Comments
Member 15073686 27-Feb-21 4:27am    
Thanks dude but I got my answers there was one silly mistake that in line

cmd.CommandText = "insert into Login (UserName, PassWord,) values ('" + textBox1.Text + "','" + textBox2.Text + "');";
i wasn't suppose to put the comma in "insert into login (Username,Password,)...."
The line needs to be like this "insert into login (Username,Password)....."

1 solution

The basic error is that your list ends with a comma:
SQL
insert into Login (UserName, PassWord,)

But you main problems are a lot more serious.

1) Despite passing parameters to SQL, your command doesn't use them! 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. Always use Parameterized 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?

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
 
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