I'm working on an WPF application. It has a couple of TextBox fields and an DataGrid.
To create the SQLite database (with only one table) I have have done this...
SQLiteCommand command = new SQLiteCommand(connection);
command.CommandText = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,name VARCHAR(100)...
And so on...
Now The query to create view is:
command.CommandText = "CREATE VIEW IF NOT EXISTS users_details AS SELECT name,adress...
And so on again.
This works flawless, but... I'm stuck on one problem. I want to connect that view "user_details" with my DataGrid.
Any guidance or part of solution would be appreciated...
You will need to create another SQLiteCommand to retrieve the data from the view and then use it as an ItemsSource for the DataGrid
SQLiteCommand command = new SQLiteCommand(connection);
command.CommandText = "SELECT * FROM users_details"
SQLiteDataAdapter da = new SQLiteDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
this.dataGrid.ItemsSource = dt.AsDataView();
Related
I have an access database in a windows forms application which I use to populate datagridviews. However one of the columns never displays and when specified within the select statement it returns a "No value given for one or more required parameters"
Here is the current code
string select_Schedules = "SELECT * FROM Schedules";
conn = new OleDbConnection(constr);
conn.Open();
ds = new DataSet();
adap = new OleDbDataAdapter();
cmd = new OleDbCommand(select_Schedules, conn);
adap.SelectCommand = cmd;
adap.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
dataGridView2.DataSource = ds.Tables[0].DefaultView;
dataGridView3.DataSource = ds.Tables[0].DefaultView;
dataGridView4.DataSource = ds.Tables[0].DefaultView;
conn.Close();
The column is indeed contained within the table
This what the "SELECT * " query returns
as seen the Show_Date column is missing. When specified through
SELECT Show_Date FROM Schedules
it returns the error as previously mentioned.
As Steve pointed out I had an old connection string in this specific form. A simple mistake indeed.
I bet that you are not looking at the same database that you are querying. Check your connectionstring. Did you use DataDirectory shortcut? – Steve
So my problem has been solved. Thank you.
I'm trying to display data from multiple tables to a data grid and let the user edit the data and have it saved in my sql server.
I get an error message: Dynamic SQL generation is not supported against multiple base tables.
I declared my variables like so:
private NpgsqlDataAdapter da;
private DataSet ds;
private NpgsqlCommandBuilder cmdBuilder;
At load I've got this code:
string SELECT = "SELECT Table_1.Col_1, Table_1.Col_2, Table_2.Col_1, Table_2.Col_2 FROM Table_1 INNER JOIN Table_2 ON Table_1.ID = Table_2.ID";
conn.Open();
da = new NpgsqlDataAdapter(SELECT, conn);
ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
And my save buton looks like this:
cmdBuilder = new NpgsqlCommandBuilder(da);
da.Update(ds);
I believe it's because SqlCommandBuilder is not suppose to work with multiple tables. Is there any other way of doing this?
I only need the data updated on Table_1 (user won't edit any data from Table_2) but I need data from both tables displayed on the datagrid.
I am developing Windows application (environment: Visual Studio 2010 and C#)
I use a datagridview with records completed by datatable dt:
dataGridView1.DataSource = dt;
This datatable has 20 columns with 1 identity column - column[0] = autonumber, and column[1] called “RecordChecked” implemented as Boolean (checkbox).
I need to solve next problems:
Select rows filtered by column[2] (in code example: DrawingNo='DM-3012');
Keep these records
Add exactly the same records below existing but update column[2] with different value like DrawingNo='DM-3013' (so we’ll have twice more records)
I started from copying records from one datatable into another (see code below) – this code works ok, but then stacked how to add copied records below existing and then update them:
DataTable dtSource = ((DataTable)dataGridView1.DataSource);
DataTable dtTarget = new DataTable();
dtTarget = ((DataTable)dataGridView1.DataSource).Clone();
DataRow[] rowsToCopy;
rowsToCopy = ((DataTable)dataGridView1.DataSource).Select("DrawingNo='DM-3012'");
foreach (DataRow temp in rowsToCopy)
{
dtTarget.ImportRow(temp);
}
dt = dtTarget;
Thanks,
I think I found a good approach to this problem.
1. Create Stored Procedure (mySP).
This SP creates temp table where we located all records selected with clause 'WHERE DrawingNo = #CopyFrom'.
Then this SP updates temp table with statement like:
UPDATE #TempTbl1 SET RecChecked = 0, DrawingNo = #CopyTo WHERE DrawingNo = #CopyFrom.
Then SP inserts updated records from temp table into the main table.
Finally SP drops temp table and selects all needed records from the main table.
2. Now we can run this SP in app and bind data to datagridview like:
//Run SP
using (SqlCommand cmd = new SqlCommand("mySP", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#CopyFrom", SqlDbType.VarChar).Value = sValueFrom;
cmd.Parameters.Add("#CopyTo", SqlDbType.VarChar).Value = sValueTo;
con.Open();
cmd.ExecuteNonQuery();
}
//Create SELECT statement
string strSelect = "SELECT Recid, RecChecked, DrawingNo, ... FROM Tbl1 WHERE DrawingNo = '" + sValueTo + "'"
//fill dataadapter
sda = new SqlDataAdapter(#strSelect, con);
dt = new DataTable();
sda.Fill(dt);
That works!
It is working fine if just display the results from a single table.
SqlCeCommand command = new SqlCeCommand("SELECT Student.EnrolNo,Student.St_Name FROM Student", ceConnection);
SqlCeDataAdapter adapter = new SqlCeDataAdapter(command);
DataSet set = new DataSet();
adapter.Fill(set, "Student");
ReportDocument document = new ReportDocument();
document.Load(#"C:\Users\user\documents\visual studio 2012\Projects\Student_Scholarship_management2\Student_Scholarship_management2\MainReport.rpt");
document.SetDataSource(set.Tables[0]);
crystalReportViewer1.ReportSource = document;
Now the above code is working fine as it just references a single table however if i use the query below which accesses record from multiple tables
SqlCeCommand command = new SqlCeCommand("SELECT Student.EnrolNo,Student.St_Name,Campus.C_ID FROM Student INNER JOIN Campus ON Student.Campus_ID=Campus.C_ID", ceConnection);
Only those columns get displayed which are from the Student Table. I have tried importing and linking more tables in through the Database Expert but it is of no use. I have verified the query and the resutls are fine and coming through just not getting displayed in crystal reports.
I have added the table in database expert and have added the relevant column in the report designer. Unfortunately its only displaying the data from one table. What could be causing this?
I would have a look at your set command:
document.SetDataSource(set.Tables[0]);
To me that seems to be setting only the first table in your DataSet.
I have a similar situation in the app I'm working on and this is my code (which is passing in about 10 tables):
var data = new DataSet();
//Do stuff to populate the dataset
report.SetDataSource(data);
The final line sets the datasource of your report to be all the tables in the DataSet.
When you call:
adapter.Fill(set, "Student");
In that moment, the table schema in dataset is built. So, if you fill below with other columns, only the columns from the first fill will be considered. A solution for you would be to replace:
SqlCeCommand command = new SqlCeCommand("SELECT Student.EnrolNo,Student.St_Name FROM Student", ceConnection);
with
SqlCeCommand command = new SqlCeCommand("SELECT Student.EnrolNo,Student.St_Name, null as C_ID FROM Student", ceConnection);
I read SQL Command Builder class from
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx and I found that I can show update done on dataset/database using select and update command.
SQL Command Builder concept is clear if I am using single dataset but what if I want to use two different dataset?
Scenario: I am reading values from database into one dataset ds1; which is assign to sql adapter and sql command builder. Now, I am reading only selected values from ds1 and storing into second dataset ds2; which is not assign to sql data adapter and sql command builder.
I am concern if I am updating any data on ds2 whether it will update database or not. Also, how should I do it using SQL Command builder and SQL Adapter.
//primary dataset
ds = new ProductDataSet();
command = new SqlCommand(
"SELECT no, name, price, cost, dept FROM PRODUCTS", connection);
adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
adapter.Fill(ds, "table");
Primary dataset is fill on form load event. User will enter item no of his choice which will be search from primary ds and saved/display onto 2nd ds (2nd ds is not connected with with any adapter or command builder right now). For eg; 2nd ds have 3 items.
Now say user update any information on 2nd ds it should automatically update database and display on grid.
//2nd ds2 update code
for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
{
string item = ds2.Tables[0].Rows[i][0].ToString();
command = new SqlCommand("UPDATE PRODUCTS SET " + _colName + " = '" + _newValue + "'" + "WHERE ITEM_NO = '" + item + "'", Class1.conn);
datagrid.DataSource = ds2.Tables[0];
}
According to your suggestion if I am adding/declaring adapter/builder in above code it doesn't work. I am getting Table Mapping error.
Use another SQLAdapter and SQLCommandBuilder. The example on that page shows how to update your database. You just need to supply the fields to be updated in the form of a query, such as:
SELECT Name, Address, Phone, Email FROM Contact
and the command builder will generate the proper SQL UPDATE statement.
SqlCommandBuilder automatically generates INSERT, UPDATE and DELETE sql statements based on the SELECT statement for a single table.
For the Transact-SQL statements to be generated using SqlCommandBuilder, there are 2 steps
Step 1. Set the "SelectCommand" property of the SqlDataAdapter object
SqlDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = new SqlCommand("SELECT_Query", con);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "Students");
Step 2. Create an instance of SqlCommandBuilder class and associate the
SqlDataAdapter object created above using DataAdapter property of the SqlCommandBuilder object
SqlCommandBuilder builder = new SqlCommandBuilder();
builder.DataAdapter = dataAdapter;
Step 3. Updating records of ds1
dataAdapter.Update(ds1, "Students");