In datagrid using C# while updating the sqlite from the form application I am getting index out of range exception .
Cannot find table 0
Below is my code
SQLiteConnection connection4 = new SQLiteConnection
(#"Data Source = C:\APTRABuilder.sqlite;Version =3");
connection4.Open();
string sql2 = "Update table set language1= '"
+ textBoxUpdate1.Text + "' where language2 = '"
+ textBox_Search.Text + "'";
SQLiteDataAdapter connect4 = new SQLiteDataAdapter(sql2, connection4);
DataSet ds4 = new DataSet();
connect4.Fill(ds4);
dataGridView.DataSource = ds4.Tables[0];
Error I am getting in dataGridView.DataSource = ds4.Tables[0];
There are no tables in the DataSet becasue the SQL is doing an UPDATE, not a SELECT, so there are no results to return.
You need to look at creating an UPDATE command for your Data Adapter.
You will also need to learn about adding values with Parameters.
Rather than trying to Fill the DataAdapter you are required to UPDATE the DataAdapter in this scenario. When you Fill a DataAdapter, you are populating the Adapter. To do this you use a SELECT command. These two methods are entirely different.
This Guide below will get you started in understanding Data Adapters better :-
http://msdn.microsoft.com/en-us/library/33y2221y.aspx
Good Luck.
Related
So i used datasource wizard in VS2012 to generate DataSet from my Database.
So now i am trying to fill in autogenerated datatable for each table in the database to be filled with data. However i am running into some problems.
string var = comboBox1.Text;
SqlDataAdapter ad = new SqlDataAdapter(#"SELECT * FROM Contacts WHERE "+var+" LIKE "+textBox1.Text+";",connection);
DataTable dataTable = new DataTable();
ad.Fill(dataTable);
ERPDataSet.ContactsDataTable dt = new ERPDataSet.ContactsDataTable(dataTable);
contactsTableAdapter.Fill(dt);
So now the problem here is That when i pass Datatables into the constructor of ContactsDataTable i get the exception null reference exception "Object reference not set to an instance of an object."
However i know for a fact that datatable is not empty.
So any help here would be appreciated.
Short answer: debug it.
Long answer: inspect the actual command text, and then compare that to what you assume by executing it with the same credentials/authority. It is a certainty that you have an incorrect assumption.
My guess without having either the values of the table or the var variable in hand, is that your LIKE clause is functionally equivalent to an equality comparison because you are missing wildcard characters in your command text that you are assuming are present at execution or even that you are missing vital string literal single quotes.
Perhaps this would work as you expect:
SqlDataAdapter ad = new SqlDataAdapter(String.Format(#"SELECT * FROM Contacts WHERE {0} LIKE '%{1}%', var, textBox1.Text), connection);
Ok so i was able to solve my problem by directly executing the query against the tableadapter instead of using separate data adapter
string var = comboBox1.Text;
contactsTableAdapter.Adapter.SelectCommand = new SqlCommand(#"SELECT * FROM Contacts WHERE " + var + " LIKE " + textBox1.Text + ";");
contactsTableAdapter.Adapter.SelectCommand.Connection = connection;
contactsTableAdapter.Adapter.Fill(eRPDataSet.Contacts);
I have a datagridview bound to a datasouce, bound to a datatable.
I want to create a button that when I click it, the database will be updated with the new parameters in the datagridview.
I've read some stuff about TableAdapter but I cant really find some good examples and explanations.
So if someone can give some information about tableadapter, it will help me a lot.
Also, if you think that you have a better solution for me regarding update the database, i'll be glad to know about that.
EDIT:
ok so i'm trying to use the mysqlcommandbuilder.
My code now looks like this:
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = new MySqlCommand("SELECT * from setups", sql_Class.myConnection);
MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
cb.GetUpdateCommand();
da.Update(dt);
so now my error is in the da.Update(dt) and it says: "input string was not in the correct format".
I've created a loop that runs on all the parameters in the sql query and all the parameters values are null.
for (int i = 0; i <= cb.GetUpdateCommand().Parameters.Count - 1; i++)
{
Console.WriteLine(cb.GetUpdateCommand().Parameters[i].ParameterName + " " + cb.GetUpdateCommand().Parameters[i].Value);
}
I have values in the datatable, i've double checked it, but someone the parameters are null.
Any ideas why?
Managed to update my database in some "old fashion" method.
I'm just running on every single row in the datagridview and updates it using executenonquery.
I get all the parameters manually.
Example:
int setup_id = Convert.ToInt16(stam.Cells["setup_id"].Value);
string setup_name = stam.Cells["setup_name"].Value.ToString();
string mySqlQuery = "UPDATE setups SET setup_name='"+setup_name+"' WHERE setup_id="+setup_id;
MySqlCommand cm = new MySqlCommand(mySqlQuery, sql_Class.myConnection);
cm.ExecuteNonQuery();
I retrieve data from database like this:
OleDbDataAdapter dataAdapter
= new OleDbDataAdapter("SELECT * "
+ " FROM myTab1, myTab2"
+ " WHERE myTab1.col1 = myTab2.col3"
, connection);//OleDbConnection connection = new OleDbConnection();
DataTable dataTable = new DataTable("myDataTable");
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
dataAdapter.Fill(dataTable);
Then I use bindingSource in order to access the data in my program, everything works perfect. But after all changes I've made in bindingSource, I need so save them into the database. How can I do this?
Well normally you'd call Update() on the DataAdapter:
dataAdapter.Update(dataTable);
But since your query "joins" two tables it may not be so easy. It may be possible if you turn your SELECT query into a proper join:
"SELECT * "
+ "FROM myTab1 "
+ "JOIN myTab2 ON myTab1.col1 = myTab2.col3"
Some other alternatives:
Write an UPDATE statement that will put the right values in the right tables and set your dataAdapter's UpdateStatement
Populate the dataset with separate tables (one per base table) and join in your app
More info from MSDN:
http://msdn.microsoft.com/en-us/library/xzb1zw3x(v=vs.80).aspx
I am trying to populate some text boxes on a form with data pulled from MySQL into a data set.
I cant seem to get the code right and could use some help
string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
MySqlConnection connection;
MySqlDataAdapter adapter;
DataTable DTItems;
connection = new MySqlConnection(ConnectionString);
try
{
//prepare query to get all records from items table
string query = "select * from spt_proposal where fr_Numer = "+ a+"";
//prepare adapter to run query
adapter = new MySqlDataAdapter(query, connection);
DataSet DS = new DataSet();
//get query results in dataset
adapter.Fill(DS);
textBox5.Text = DS.Tables[0].Rows[0].ToString();
This does not hit the text box at all.
a in the query is a variable that is pulled from a different form
and the query should pull 32 different things from the database in a row
There are about 9 text boxes on the form that i will have to fill with different data from this row.
Anyone have a better way to do this?
Brent
Try:
textBox5.Text = DS.Tables[0].Rows[0][0].ToString();
Remember, that a Table is pretty much a two dimensional array. Your code literally gets to the 1st row in the DataSet, but doesn't take into account the Column.
I have a program written in c# visual studio 2008 with SQL server 2005 (.mdf) database.
Here is part of the code:
...
SqlCommandBuilder cb;
cb = new SqlCommandBuilder(dataAdapter);
String[] dataList =new String [8];
...
DataTable resultTable = new DataTable();
FillDataList(data);
dataAdapter = new SqlDataAdapter("insert into ProcessData values('" + dataList[0] + "','" + dataList[1] + "','" + dataList[2] + "','" + dataList[3] + "','" + dataList[4] + "','" + dataList[5] + "','" + dataList[6] + "','" + dataList[7]+"')", con);
dataAdapter.Fill(resultTable);
...
My questions are:
1) Where is the data I added in those lines stored?
2) Why is it that when I Right-Click with the mouse in the Server Explorer->Data Connections->Tables->ProcessData (my table's name)->"show Table Data", I don't see the data but just NULL in the columns, and how can i see the data there?
3) Why when I present this data in a DataGridView sometimes it shows the data and sometimes it doesn't?
Many Thanks!
Without seeing more info on the datalist object and its construction, it's impossible to say. However, you're using the Fill and SQLDataAdapter incorrectly. The FILL relies on the SQLDataAdapter having a SelectCommand property set, which is what your code is bunging the INSERT statement into (that's what the SQLDataAdapter's constructor does). So...your Fill returns nothing as there's no SELECT where there should be.
Your INSERT should be part of the dataadapter's InsertCommand, and you'll need to write a separate SELECT statement to get anything into your resultTable.
I think you need to read some basic documentation on ADO.NET before you write any more code. You're passing an INSERT statement to the SqlDataAdapter constructor, which takes a SELECT statement. You're using the Fill method of SqlDataAdapter where you should be using the Update method. You're building a SQL string with hard-coded values in it where you should be using SqlParameters with references to the DataTable Columns. And is the fact that your DataTable is called "data" but that SQL string is using indexed properties from "dataList" just a typo? Because if it is, DataTable doesn't have an indexed property.
Try doing it like this (you need to fill in the correct connection string though):
// Get the db connection
SqlConnection dbCon = new SqlConnection("connection string");
// Select the data from the database table into a DataSet (even if it's empty)
DataSet myData = new DataSet();
SqlDataAdapter dbAdapter = new SqlDataAdapter("select * from ProcessData", dbCon);
dbAdapter.Fill(myData);
myData.Tables[0].TableName = "ProcessData"; // Keeps the table name consistent for the DataMember property
// Use the command builder to add insert, delete, update commands to your adapter
// You must have a primary key on the table for these to work, though
SqlCommandBuilder dbComBuilder = new SqlCommandBuilder(dbAdapter);
dbAdapter.InsertCommand = dbComBuilder.GetInsertCommand();
dbAdapter.DeleteCommand = dbComBuilder.GetDeleteCommand();
dbAdapter.UpdateCommand = dbComBuilder.GetUpdateCommand();
// Bind the data set to the GridView for viewing / editing
yourGridControl.AutoGenerateColumns = true; // Optional, if you haven't manually added the columns
yourGridControl.DataSource = myData;
yourGridControl.DataMember = "ProcessData";
// Use the db adapter to update the database (by calling those commands) with
// changes made to the DataSet through the grid. This would go in a different
// form event, like a Save Button click.
dbAdapter.Update();
If you don't have a primary key in the ProcessData table, you can directly insert values using this command:
// Insert the data directly with a command
SqlCommand dbInsCommand = new SqlCommand("insert into ProcessData values (" + val1 + "," + val2 + ")", dbCon);
dbInsCommand.ExecuteNonQuery();