I'm trying to update sqlite db file in c# - c#

Now I'm trying to solve this problem. But I don't know why it didn't work.
I've finished to change data in datagridview form, but the sqlite file in desktop wasn't changed.
namespace Ex1
{
public partial class Form1 : Form
{
int initValue = 0;
String chkString;
SQLiteConnection conn = new SQLiteConnection("Data Source=I:\\Coding\\VS\\Study1\\Ex1\\Ex1\\Test.db;Version=3;");
SQLiteDataAdapter adapter = null;
BindingSource bsOrigin = null;
DataSet ds = null;
Random rand = new Random();
KnownColor[] Colors = (KnownColor[])Enum.GetValues(typeof(KnownColor));
KnownColor randColor;
public Form1()
{
InitializeComponent();
textBox1.Text = initValue.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
bsOrigin = new BindingSource();
ds = GetData();
bsOrigin.DataSource = ds.Tables[0];
dataGridView1.DataSource = bsOrigin;
}
private DataSet GetData()
{
adapter = new SQLiteDataAdapter("SELECT * from Skill", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
adapter = new SQLiteDataAdapter("SELECT * from Skill", conn);
BindingSource bs = (BindingSource)dataGridView1.DataSource;
DataTable dt = bs.DataSource as DataTable;
adapter.Update(dt);
}
}
}
Is there anyone who can teach me?

I use this method:
public void saveData ()
{
SQLiteDataConnection con = new SQLiteDataConnection("yourconnectionstring");
SQLiteDataAdapter da= new SQLiteDataAdapter("Select statement", con);
SQLiteCommandBuiler com=new SQLiteCommandBuiler(da);
DataSet ds= new DataSet ();
DataTable dt = new DataTable ():
DataRow dr;
con.Open();
da.fill(ds, "tablename");
cn.Close();
dt=ds.Tables["tablename"];
dr=dt.NewRow();
dr["rowname"]=value;
//"same for the other rows"
dr.Rows.Add(dr);
da.Update(dt.Select(null, null, DataViewRowState.Added));
}
you should also check the file permission, I have seen issues with sqlite when they are access from certain folders, the desktop was one.

Related

I want to us a value from the URL as the condition of my sql data source sql Statement

This is my code, but when I run it, I get no results:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
DataSet ds = GetData();
r1.DataSource = ds;
r1.DataBind();
}
}
Code to populate the repeater starts here:HERE
private DataSet GetData()
{
//GET VALUE FROM URL
this.Target = Request.QueryString["Loctn"];
Citytxt.Text = Target;
String cs = "Data Source=KISAKA;Initial Catalog=ACCOMMODATION;Integrated Security=True;";
using (SqlConnection con = new SqlConnection(cs))
{
SqlDataAdapter da = new SqlDataAdapter("Select * from ACC_Facility where Location_City = '"+ Target +"'", con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}

how to solve "dynamic sql generation is not supported against multiple base tables" Error?

I got this error "dynamic sql generation is not supported against multiple base tables" in my project.
i got the error on "da.update(dt)" line.
this is the code which i got the error...`so help me how can i solve it..
OleDbDataAdapter da;
DataSet ds = null;
BindingSource bsource = new BindingSource();
OleDbCommand cmd;
public void BindData()
{
cmd = new OleDbCommand("select t.srno,c.AccNumber2,c.Name,t.Admission_Fee,t.Family_fund,t.MonthlyCollection,t.MonthlyDeposit from transDemand t inner join Customer c on t.AccNumber=c.AccNumber where t.IssueDate=#IssueDate and c.Dismember=false", con);
cmd.Parameters.Add("#IssueDate", OleDbType.Date).Value = datesearch.Value.ToString("dd-MM-yyyy");
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
//dt = new DataTable();
//da.Fill(dt);
OleDbCommandBuilder cmdbuild = new OleDbCommandBuilder(da);
da.Fill(ds, "A");
bsource.DataSource = ds.Tables["A"];
dataGridView1.DataSource = bsource;
}
private void btnupdate_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["A"];
this.dataGridView1.BindingContext[dt].EndCurrentEdit();
da.Update(dt);
BindData();
}

How to call WebMethod service in WindowForm

How to update server data through a webservice by using ADO.NET and Visual C#.NET
WebMethod
public void UpdateDeMo1(DataSet ds)
{
LayerAccess ac = new LayerAccess();
string[] parameters = { };
object[] values = { };
SqlDataAdapter da = new SqlDataAdapter();
da = ac.getDataAdapter("P_Demo", parameters, values);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(ds.Tables[0]);
//return ds;
}
But the error occurs when calling WebMethod.
private void simpleButton1_Click(object sender, EventArgs e)
{
//ud.AutoUpdateDemo(gridControl1);
DataTable dt = new DataTable();
DataTable dtt = new DataTable();
dt = (DataTable)gridControl1.DataSource;
dtt = dt.Copy();
DataSet ds = new DataSet("B");
ds.Tables.Add(dtt);
DataSet dschang = new DataSet();
dschang = ds.Copy();
sv.UpdateDeMo1(dschang.Copy());
}
Please help me.

characterized search using windows form application

I have a search textbox in my windows form application and I want to search character by character means when I write h in textbox then shows result in datagridview of h and when I add h with a like ha in search textbox then shows result in datagridview of ha and its change results in datagridview from h to ha as same as mobile phone contacts searching. and I working like below:
public partial class Form2 : Form
{
SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\Administrator\\Documents\\Contact.mdf;Integrated Security=True;");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
bindDatagridview();
if (textBox1.Text != string.Empty)
{
search();
}
}
public void bindDatagridview()
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = new SqlCommand("Select * from contactsinfo", connection);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
//dataGridView1.DataBindings();
}
public void search()
{
da.SelectCommand = new SqlCommand("Select * from contactsinfo where ContactName = '" + textBox1.Text + "'", connection);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
clear();
}
}
But this only work when the form is load and the form is only loaded at first time:
Kindly suggest me what i do, waiting for your reply.
Thanks.
If you can load all contacts at once, then its simple. On form load get all contacts and save them in DataView. Then bind grid to this view:
DataView contactsView;
private void Form2_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
var da = new SqlDataAdapter("SELECT * FROM from contactsinfo", conn);
da.Fill(dt);
}
contactsView = dt.AsDataView();
dataGridView1.DataSource = contactsView;
}
Then just change row filter of DataView when text changes in filter TextBox:
private void textBox1_TextChanged(object sender, EventArgs e)
{
contactsView.RowFilter =
String.Format("ContactName LIKE '{0}%'", textBox1.Text);
}
If you can't pre-load all data, then you should use same TextChanged event to query for filtered data.
NOTE: You should handle ' in user input.

Update database from datagridview

I have googled a lot on updating datagridview to database after editing using the adapter. I referred to several websites that gave me similar examples like the one below. However, I'm getting the "ArgumentNullException was unhandled" error at the first line of my button2_Click.
I am new to programming and I have been taught to declare the adapter as a global, and I did. Why am I still getting null value? Any help would be appreciated. Thank you!
DataTable dt;
DataSet ds;
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
public void button1_Click(object sender, EventArgs e)
{
//Bind button
string txt = textBox1.Text;
string strOleDbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Project.mdb";
string strSqlStatement = string.Empty;
strSqlStatement = "SELECT * FROM jiahe WHERE [User] = '" + txt + "'";
OleDbConnection objConnection = new OleDbConnection(strOleDbConnectionString);
objAdapter = new OleDbDataAdapter(strSqlStatement, objConnection);
DataSet ds = new DataSet();
dataGridView1.DataSource = ds;
objAdapter.Fill(ds);
DataTable dt = ds.Tables[0];
dataGridView1.DataSource = dt.DefaultView;
try
{
if (dt.Rows.Count == 1)
{
MessageBox.Show("Record found.");
}
else
{
if (dt.Rows.Count == 0)
{
MessageBox.Show("Invalid input!");
}
}
}
catch
{
MessageBox.Show("Error!");
}
}
private void button2_Click(object sender, EventArgs e)
{
objAdapter.Update(ds);
dataGridView1.DataSource = ds;
}
I think it's simply a confusion between the variables in different scopes. The ds you use in the second button_click is not the same data set you use to populate a grid . Make them the exactly same instance and it will, most likely work.
Edit
to make so should be enough to write instead of
DataSet ds = new DataSet();
dataGridView1.DataSource = ds;
objAdapter.Fill(ds);
DataTable dt = ds.Tables[0];`
This
ds = new DataSet();
dataGridView1.DataSource = ds;
objAdapter.Fill(ds);
DataTable dt = ds.Tables[0];`
I encountered the same problem and I fixed it by trying this:
DataSet ds = new DataSet();
dataGridView1.DataSource = ds;
objAdapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];

Categories

Resources