Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
my code behind code


C#
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;
using System.Data.SqlClient;

namespace usesqlitecsharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Data_connection dbobject = new Data_connection();
            SQLiteConnection SQLconnect = new SQLiteConnection();
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SQLconnect.ConnectionString = dbobject.datalocation();
            SQLconnect.Open();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            SQLiteCommand SQLcommand = new SQLiteCommand();
            SQLcommand = SQLconnect.CreateCommand();
            SQLcommand.CommandText = "CREATE TABLE IF NOT EXISTS " + textBox1.Text + "( Username TEXT, Password TEXT);";
            SQLcommand.ExecuteNonQuery();
            SQLcommand.Dispose();

            MessageBox.Show("Table Created");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SQLiteCommand cmd = new SQLiteCommand();
            cmd = SQLconnect.CreateCommand();
            cmd.CommandText = "insert into " + textBox1.Text + " (Username,Password)  values (@username,@password)";
            cmd.Parameters.AddWithValue("@username", textBox2.Text);
            cmd.Parameters.AddWithValue("@password", textBox3.Text);
            cmd.ExecuteNonQuery();
            cmd.Dispose();

            MessageBox.Show("Data Inserted");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SQLiteCommand cmd = new SQLiteCommand("select * from " + textBox1.Text, SQLconnect);
            SQLiteDataAdapter da = new SQLiteDataAdapter();
            DataTable dt = new DataTable();
            da.SelectCommand = cmd;
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("No Data Exist in Table");
            }
        }
    }
}


dotaconnection.cs code is 


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

namespace usesqlitecsharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Data_connection dbobject = new Data_connection();
            SQLiteConnection SQLconnect = new SQLiteConnection();
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SQLconnect.ConnectionString = dbobject.datalocation();
            SQLconnect.Open();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            SQLiteCommand SQLcommand = new SQLiteCommand();
            SQLcommand = SQLconnect.CreateCommand();
            SQLcommand.CommandText = "CREATE TABLE IF NOT EXISTS " + textBox1.Text + "( Username TEXT, Password TEXT);";
            SQLcommand.ExecuteNonQuery();
            SQLcommand.Dispose();

            MessageBox.Show("Table Created");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SQLiteCommand cmd = new SQLiteCommand();
            cmd = SQLconnect.CreateCommand();
            cmd.CommandText = "insert into " + textBox1.Text + " (Username,Password)  values (@username,@password)";
            cmd.Parameters.AddWithValue("@username", textBox2.Text);
            cmd.Parameters.AddWithValue("@password", textBox3.Text);
            cmd.ExecuteNonQuery();
            cmd.Dispose();

            MessageBox.Show("Data Inserted");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SQLiteCommand cmd = new SQLiteCommand("select * from " + textBox1.Text, SQLconnect);
            SQLiteDataAdapter da = new SQLiteDataAdapter();
            DataTable dt = new DataTable();
            da.SelectCommand = cmd;
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("No Data Exist in Table");
            }
        }
    }
}



error is


Error	5	The name 'SQLconnect' does not exist in the current context	c:\users\srikanth\documents\visual studio 2012\Projects\usesqlitecsharp\usesqlitecsharp\Form1.cs	33	26	usesqlitecsharp
Posted
Updated 17-Sep-14 0:30am
v2
Comments
[no name] 17-Sep-14 6:30am    
And? We are supposed to debug your unformatted code for you? The error tells you exact what and where the problem is, all you have to do is go fix it. And no, it's not at all urgent.
harshavardhan12345678 17-Sep-14 23:48pm    
Now the Error is Unable to Open the Database
harshavardhan12345678 17-Sep-14 8:54am    
now i did another sqlite sample same but getting error is


by clicking button it get like this Could not load file or assembly 'System.Data.SQLite, Version=1.0.94.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Quote:
public Form1()
{
Data_connection dbobject = new Data_connection();
SQLiteConnection SQLconnect = new SQLiteConnection();
InitializeComponent();
}

In the above code you create a SQLiteConnection object that is
  • Unavailable in other parts of your code
  • Candidate to garbage collection

You should declare SQLconnect as class member.
 
Share this answer
 
Comments
harshavardhan12345678 17-Sep-14 6:40am    
now iam getting error like this
Error 2 Expected class, delegate, enum, interface, or struct c:\users\srikanth\documents\visual studio 2012\Projects\usesqlitecsharp\usesqlitecsharp\Form1.cs 35 17 usesqlitecsharp
harshavardhan12345678 17-Sep-14 23:49pm    
now iam getting error like this = Unable to Open the Database
In both constructors you're using local variable

SQLiteConnection SQLconnect = new SQLiteConnection();

which goes out of scope after the constructor finishes. You have to create class member variable:

C#
public partial class Form1 : Form
{
    SQLiteConnection _SQLconnect;

    public Form1()
    {
        Data_connection dbobject = new Data_connection();
// Now instantiate the variable that will be available to every method within your form
        _SQLconnect = new SQLiteConnection();
        InitializeComponent();
    }

}
 
Share this answer
 
Comments
harshavardhan12345678 17-Sep-14 6:41am    
now iam getting error like this
Error 2 Expected class, delegate, enum, interface, or struct c:\users\srikanth\documents\visual studio 2012\Projects\usesqlitecsharp\usesqlitecsharp\Form1.cs 35 17 usesqlitecsharp
Sinisa Hajnal 17-Sep-14 7:10am    
On which line? Please update your question.

Also, pick one solution and accept it, your question is answered (although everyone gave the same answer... :) )
harshavardhan12345678 17-Sep-14 23:49pm    
Now iam getting error like this Unable to Open the Database
Sinisa Hajnal 18-Sep-14 2:02am    
After less then a minute of googling:
http://www.pantz.org/software/sqlite/unabletoopendbsqliteerror.html

This is last answer you'll get from me. Learn how to learn, man! At least make an effort before you go asking for help. My vote of 1.
harshavardhan12345678 18-Sep-14 2:21am    
dont mind iam a fresher thats y iam asking ...
try to declare it away from the consturctor as a class member , then you can initialize it in the constructor as follows
C#
public partial class Form1 : Form
{
    SQLiteConnection SQLconnect;
    public Form1()
    {
        Data_connection dbobject = new Data_connection();
        SQLconnect = new SQLiteConnection();
        InitializeComponent();
    }


so you can have access to it at any level in that classd
 
Share this answer
 
Comments
harshavardhan12345678 17-Sep-14 23:50pm    
Now iam getting error like this Unable to Open the Database

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