SQL query acessing multiple tables - c#

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);

Related

Combine two data fields(columns) values and display in Listbox-ASP.Net?

I want to select data from Microsoft SQL Server and concatenate two of its column values and display it in ListBox.
For example - there are two columns in SQL, namely Software name and Software-users. A single software has N number of users what I want to do is select the software name in SQL and concatenate it with Software users name and display it in the listbox.
So far I tried this, but the listbox displays null.
SqlCommand cmd = new SqlCommand("Select [SWName] +'-'+ [SWUser] as Creator from AllSWUsers WHERE SWName = #SWName", con);
cmd.Parameters.AddWithValue("#SWName", Global.usrlsts);
SqlDataAdapter daFill = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
daFill.Fill(ds);
lstUsers.DataSource = ds;
lstUsers.DataTextField = "Creator";
lstUsers.DataValueField = "SWName";
lstUsers.DataBind();
It may be the reason you are not selecting SWName in your query but you are using that column as shown below in your code.
lstUsers.DataValueField = "SWName";
Try this
Select ISNULL([SWName], '') as [SWName], ISNULL([SWName],'') +'-'+ ISNULL([SWUser], '') as Creator from AllSWUsers WHERE SWName = #SWName

Updating data in multiple tables from datagrid

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.

How to update two data tables back to db using OleDb and DataAdapter in c#

I got two datatables from a mdb file using oledb and modified it.
accessConnection.Open();
string selectQuery = "SELECT * FROM Students";
DataAdapter = new OleDbDataAdapter(selectQuery, accessConnection);
DataAdapter.Fill(StudentsDataTable);
DataAdapter.SelectCommand.CommandText = "SELECT * FROM Teachers";
DataAdapter.Fill(TeachersDataTable);
// modified the two datatables
// ...
However, I have a problem in updating the two tables back into the mdb file. I can't update StudentsDataTable because the DataAdapter select command was already changed to select TeachersDataTable. When I handle only one DataTable, I don't have this kind of problem.
DataAdapter.UpdateCommand = new OleDbCommandBuilder(DataAdapter).GetUpdateCommand();
DataAdapter.Update(StudentsDataTable); <-- exception error occur that columns don't match.
DataAdapter.Update(TeachersDataTable);
Do you have any ideas for updating two datatables using one DataAdapter? or Should I have separate DataAdapters for the two tables?
Essentially you have to create two instances.
DataAdapter.UpdateCommand = new OleDbCommandBuilder(DataAdapter).GetUpdateCommand();
DataAdapter.Update(TeachersDataTable);
DataAdapter.SelectCommand.CommandText = "SELECT * FROM Students";
DataAdapter.UpdateCommand = new OleDbCommandBuilder(DataAdapter).GetUpdateCommand();
DataAdapter.Update(StudentsDataTable);
The main issue is your DataAdapter is set to Teachers table as it was the last command set on it.

Cannot commit DataSet changes to database

I've written a small form that reads the data from a database table (SQL CE 3.5) and displays it in a DataGridView control. This works fine. I then modified it to make a change to the data before displaying it, which also seems to work fine with the exception that it doesn't seem to actually commit the changes to the database. The code is as follows:
using (SqlCeConnection conn = new SqlCeConnection(
Properties.Settings.Default.Form1ConnectionString
)) {
conn.Open();
using (SqlCeDataAdapter adapter = new SqlCeDataAdapter(
"SELECT * FROM People", conn
)) {
//Database update command
adapter.UpdateCommand = new SqlCeCommand(
"UPDATE People SET name = #name " +
"WHERE id = #id", conn);
adapter.UpdateCommand.Parameters.Add(
"#name", SqlDbType.NVarChar, 100, "name");
SqlCeParameter idParameter = adapter.UpdateCommand.Parameters.Add(
"#id", SqlDbType.Int);
idParameter.SourceColumn = "id";
idParameter.SourceVersion = DataRowVersion.Original;
//Create dataset
DataSet myDataSet = new DataSet("myDataSet");
DataTable people = myDataSet.Tables.Add("People");
//Edit dataset
adapter.Fill(myDataSet, "People");
people.Rows[0].SetField("name", "New Name!");
adapter.Update(people);
//Display the table contents in the form datagridview
this.dataGridView1.DataSource=people;
}
}
The form displays like so:
Looking at the table via Visual Studio's Server Explorer however, doesn't show any change to the table.
What am I doing wrong?
I found it. It took days but I found it.
Properties.Settings.Default.Form1ConnectionString is "Data Source=|DataDirectory|\Form1.sdf". The update works if I replace the automatically generated "|DataDirectory|" with the actual path. Oddly enough reading from the database works either way.
Shouldn't the update line be
adapter.Update(myDataSet, "People")
I would make sure the DataSet believes it's been changed. Invoke DataSet.HasChanges (returns bool) and DataSet.GetChanges, which returns a delta of the DataSet from the original.
Have you also tried this against Sql Server Express just to eliminate any issues with the CE data adapter?

Single Query returning me 4 tables, How to get all of them back into dataset?

How to fill multiple tables in a dataset.
I m using a query that returns me four tables.
At the frontend I am trying to fill all the four resultant table into dataset.
Here is my Query. Query is not complete. But it is just a refrence for my Ques
Select * from tblxyz compute sum(col1)
suppose this query returns more than one table, I want to fill all the tables into my dataset
I am filling result like this
con.open();
adp.fill(dset);
con.close();
Now when i checks this dataset. It shows me that it has four tables but only first table data is being displayed into it. rest 3 dont even have schema also.
What i need to do to get desired output
Use DataAdapter.TableMappings.
e.g. :
DataSet ds = new DataSet();
// setup DataSet if required
SqlCommand cmd = new SqlCommand();
// setup command
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.TableMappings.Add("Table", "goofy");
da.TableMappings.Add("Table1", "donald");
da.TableMappings.Add("Table2", "daffy");
da.TableMappings.Add("Table3", "foghorn");
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Fill(ds);
where "goofy" is the name of the DataTable you want your first result set to go into,
and "donald" is the second etc.
Check this links.
http://www.developer.com/article.php/3311341
http://vb.net-informations.com/dataset/dataset-multiple-tables-sqlserver.htm
Maybe the problem is that you need to return all your tables separetly, as different queries, for example, fill-ing the same DataSet in a loop.
Good Luck!

Categories