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];
Related
I need guidance and code examples to compare the Specific values returned from "show slave status" to a string every 5 minutes. And if the value doesn't match up it sends a query to restart.
This is what i have for now:
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
DataSet ds = new DataSet();
private DataTable dt;
public void BindData()
{
MySqlConnection con = new MySqlConnection("server = 127.0.0.1; user id = system; persistsecurityinfo = True; port = 3308; database = test1; Password = 975315");
con.Open();
MySqlCommand cmd = new MySqlCommand("show slave status;", con);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
adp.Fill(ds); //Fill Dataset
dt = ds.Tables[0]; //Then assign table to dt
ds.Tables[0].DefaultView.RowFilter = "";
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
cmd.Dispose();
con.Close();
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();
}
I want to show content of a table("newexample") in my database in SQL server to gridview in ASP.net, but the gridview shows nothing. How to fix it?
Here is the code:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string cs = ConfigurationManager.ConnectionStrings["SampleDBCS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("select * from newexample", con);
DataTable dt = new DataTable();
dt.Columns.Add("col0");
dt.Columns.Add("col1");
dt.Columns.Add("col2");
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
DataRow dr = dt.NewRow();
dr["col0"] = rdr["col0"];
dr["col1"] = rdr["col1"];
dr["col2"] = rdr["col2"];
dt.Rows.Add(dr);
}
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
Did you get any exception? Try moving con.Close() after DataBind() and see how it goes.
Try to use the code below:
DataSet dataSet = new DataSet("newexample");
SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from newexample", connectionString);
dataAdapter.Fill(dataSet);
if (dataSet != null)
{
if (dataSet.Tables[0].Rows.Count != 0)
{
GridView1.DataSource = dataSet ;
GridView1.DataBind();
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
}
}
This is a better approach since SqlDataAdapter will take care for SqlConnection. It is responsible to create the connection and to close it at the end of operation in the best way possible.
This is not a tested code so if you have any problem let me know..
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.
I want to show my cart details on a Button_Click_Event. I'm using a Gridview control of ASP.NET framework. But the following exception occurs:
object reference is not set to a instance of an object.
How can I fix it? Here is my code:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string st = "select * FROM cart_master";
cmd = new SqlCommand(st, sqlcon);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
GridView GridView1 = (GridView)ContentPlaceHolder1.FindControl("GridView1");
sda.Fill(ds, "cart_master");
GridView1.DataSource = ds.Tables["cart_master"];
GridView1.DataBind();
cmd.Connection.Close();
}
Rather than dataset, just fill a datatable from your adapter
DataTable dataTable = new DataTable("ResultDataTable");
sda.Fill( dataTable);
GridView1.DataSource = dataTable;