Select statement Displaying incorrect results - c#

I implemented a search of my nested gridview, and All is working well. But when the gridview loads, it is displaying duplicate rows in the parent table.
As you can see in the picture, There are 2 books under the CourseID of AC107. But my Gridview is displaying a row for each textbook in the course. I have messed with this select statement and anyway I alter it to see if anything works, the gridview doesnt load.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//i'm using a datatable for storing all the data
DataTable dt = new DataTable();
string query = "select * from Course inner join textBooks on textBooks.CourseID = Course.CourseID";
//wrapping in 'using' means the connection is closed an disposed when done
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HUTDMSConnectionString"].ToString()))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
{
try
{
//fill the datatable with the contents from the database
adapter.Fill(dt);
}
catch
{
}
}
//save the datatable into a viewstate for later use
ViewState["allBooks"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Below is a layout of my data tables.

Are you sure your SQL query not fetching you the duplicate rows, use ROW_NUMBER() function and see how it works like (assuming you are using SQL Server since you are using SqlConnection provider class)
SELECT * FROM (
select Course.*,
ROW_NUMBER() OVER(PARTITION BY Course.CourseID ORDER BY Course.CourseID) AS rn
from Course
inner join textBooks
on textBooks.CourseID = Course.CourseID ) xxx
WHERE rn = 1;

Related

Query from multiple tables in C#

I am trying to query from multiple tables in one query.
Although my code is working for one table, I don't know how to search in the second table too.
using (DataTable dt = new DataTable("Uniclass2015_EF_v1_12; Uniclass2015_En_v1_26"))
{
using (SqlCommand cmd = new SqlCommand (" select *from Uniclass2015_En_v1_26 where title like #Title; select *from Uniclass2015_EF_v1_12 where title like #Title", conn))
{
cmd.Parameters.AddWithValue("code", txtSearch.Text);
cmd.Parameters.AddWithValue("Title", string.Format("%{0}%", txtSearch.Text));
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
dataGridView1.DataSource = dt;
}
}
conn.Close();
private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13) // enter
BtnSearch.PerformClick();
}
I tried to add a second table to my original code, but however when I type in the search box my query I receive a blank answer.
looks like you're wanting to use the UNION operator in SQL:
https://www.w3schools.com/sql/sql_union.asp
SELECT * FROM Uniclass2015_En_v1_26 WHERE title LIKE #Title UNION
SELECT * FROM Uniclass2015_EF_v1_12 WHERE title LIKE #Title
This of course assumes the columns of the two tables are the same. If you only need specific columns from each, just select those columns.
Also, I don't see where you're using that code parameter you're adding

Loading DataTable with multiple tables select query

I never used select queries with multiple tables involved before and now when I do, I'm having troubles with getting the information from the DataTable.
I have this query:
SELECT *
FROM [Usergroups], [Groups]
WHERE [Usergroups.UserID] = #name
AND [Groups.GroupID] = [Usergroups.GroupID]
And this is how I get the returned values into a DataTable:
DataTable groupsTable = new DataTable();
groupsTable.Load(sqlCmd.ExecuteReader());
Now, how can I specify my DataTable from which table I want to take rows from? For example, this is what I did before multiple tables where involved:
string groupName = groupsTable.Rows[0]["Name"];
I could not find any resource with such information, but I know it's a basic question. Thanks in advance.
The query in your question doesn't produce, as a result, multiple tables.
It produces a JOIN between two tables.
As a consequence, on the C# side, you don't have two tables but just one as before, with the all fields from both tables.
As a side note, a better way to JOIN tables together is through the use of the JOIN statement like this
SELECT * -- a field list is better here ---
FROM Usergroups ug INNER JOIN Groups g ON g.GroupID=ug.GroupID
WHERE ug.UserID=#name
and you should add, to the SELECT clause, a list of the fields that you are really interested in.
SEE a simple JOIN reference
If you want to retrieve the values of the two tables in separate DataTable objects, then you need to use a DataSet in this way
DataSet ds = new DataSet();
DataTable dtUserGroups = new DataTable("UserGroups");
DataTable dtGroups = new DataTable("Groups");
ds.Tables.Add(dtUserGroups );
ds.Tables.Add(dtGroups);
using(SqlCommand cmd = new SqlCommand("SELECT * FROM UserGroups;SELECT * from Groups", con))
{
using(SqlDataReader dr = cmd.ExecuteReader())
{
ds.Load(dr, LoadOption.OverwriteChanges, dtUserGroups, dtGroups);
// Now you have the two tables filled and
// you can read from them in the usual way
}
}
This last example could further enhanced adding a DataRelation object to the DataSet to represent the relationship between the two tables. This could allow your code to navigate the parent/child recordset.
You may try in this way :
string query = "SELECT U.ID,U.NAME, C.NAME AS CUSTOMERNAME, C.DOB FROM USER U INNER JOIN CUSTOMER C ON U.ID = C.USERID"
SqlConnection conn = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
Above code will return you one DataTable with data from two different tables say "User" and "Customer".
I hope now you know how to access data from a DataTable.
It is better to use JOIN for combining multiple tables such as INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL JOIN as per your requirements. So, when you use INNER JOIN, it will have the columns of two joined tables, i.e., if
tblA has a, b,c as columns and
tblB has a, e,f as columns
then the inner joined table will have a, b, c, e, f as its columns.
Then, you can use like this:
public DataTable LoadData()
{
DataTable dataTable;
string connString = #"your connection string here";
string query = "SELECT * FROM Usergroups t1 INNER JOIN Groups t2 ON t2.GroupID=t1.GroupID WHERE t1.UserID=#name";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(cmd);
// this will query your database and return the result to your datatable
da.Fill(dataTable);
conn.Close();
return dataTable;
}
After Getting the DataTable, then you can use this table like:
DataTable dt = LoadDataClass.LoadData();
string groupName = dt.Rows[0]["Name"]; //For first row
I hope you get it.
in wpf c#, this method can also be used to retrieve data from multiple tables
try
{
using (SqlConnection conn = new SqlConnection(_pageDataBase.connectionString()))
{
conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter Usergroups= new SqlDataAdapter("select *from Usergroups", conn);
Usergroups.Fill(dt);
SqlDataAdapter Groups= new SqlDataAdapter("select *from Groups", conn);
Groups.Fill(dt);
datagridName.ItemsSource = dt.DefaultView;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

How to display distinct data from MySql in dropdownlist box using C# asp.net?

Here I am trying to load the dropdownlist box from a column of a table in Mysql(which contains repeated values), so I need to get unrepeated values.
This is my code:
MySqlConnection cn = new MySqlConnection("Connection String");
MysqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
cn.Open();
cmd = cn.createcommand();
cmd.CommandText = "Select Columnname from tablename";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
DropDownList.Items.Add(reader.GetString("Columnname"));
}
}
cn.close();
}
Try editing your SQL query to get DISTINCT results
cmd.CommandText = "SELECT DISTINCT Columnname FROM tablename";
The code snippet is then presumably called more than once, maybe on each post back. Just clear the items first:
DropDownList.Items.Clear();
One thing to note is that when ViewState is enabled there is no need to reload your drop down lists on each subsequent post back. That also means that you can decide rather to execute this code only if if (!this.IsPostBack)`.
table1:
id name
1 saravanan
2 karumbasalam G
3 saravanan
select distinct name from table1
output:
name
saravanan
karumbasalam G
Use the distinct keyword used to avoid duplicates

add row to a BindingSource gives different autoincrement value from whats saved into DB

I have a DataGridView that shows list of records and when I hit a insert button, a form should add a new record, edit its values and save it.
I have a BindingSource bound to a DataGridView. I pass is as a parameter to a NEW RECORD form so
// When the form opens it add a new row and de DataGridView display this new record at this time
DataRowView currentRow;
currentRow = (DataRowView) myBindindSource.AddNew();
when user confirm to save it I do a
myBindindSource.EndEdit(); // inside the form
and after the form is disposed the new row is saved and the bindingsorce position is updated to the new row
DataRowView drv = myForm.CurrentRow;
avaliadoTableAdapter.Update(drv.Row);
avaliadoBindingSource.Position = avaliadoBindingSource.Find("ID", drv.Row.ItemArray[0]);
The problem is that this table has a AUTOINCREMENT field and the value saved may not correspond the the value the bindingSource gives in EDIT TIME.
So, when I close and open the DataGridView again the new rowd give its ID based on the available slot in the undelying DB at the momment is was saved and it just ignores the value the BindingSource generated ad EDIT TIME,
Since the value given by the binding source should be used by another table as a foreingKey it make the reference insconsistent.
There's a way to get the real ID was saved to the database?
I come up with this solution
First added a GetNextID() method directly to the table model:
SELECT autoinc_next
FROM information_schema.columns
WHERE (table_name = 'Estagio') AND (column_name = 'ID')
and whener I need a new row to be added I do
EstagioTableAdapter ta = new EstagioTableAdapter ();
nextID = ta.GetNextID();
row = (DataRowView)source.AddNew();
row.Row["ID"] = nextID;
(...)
source.EndEdit();
The same thing happens with Access databases. There is a great article (with solution) here. Basically, the TableAdapter normally sends 2 queries in a batch when you save the data. The first one saves the data and the second one asks for the new ID. Unfortunately, neither Access nor SQL CE support batch statements.
The solution is to add an event handler for RowUpdated that queries the DB for the new ID.
based on my answer on concurrency violation, use da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord.
note: just change SQLiteConnection and SQLiteDataAdapter to MSSQL ones, and change the LAST_INSERT_ROWID() to SCOPE_IDENTITY()
const string devMachine = #"Data Source=C:\_DEVELOPMENT\__.NET\dotNetSnippets\Mine\TestSqlite\test.s3db";
SQLiteConnection c = new SQLiteConnection(devMachine);
SQLiteDataAdapter da = new SQLiteDataAdapter();
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
da = new SQLiteDataAdapter("select product_id, product_name, abbrev from product", c);
var b = new SQLiteCommandBuilder(da);
da.InsertCommand = new SQLiteCommand(
#"insert into product(product_id, product_name, abbrev) values(:_product_id, :_product_name, :_abbrev);
select product_id /* include rowversion field here if you need */
from product where product_id = LAST_INSERT_ROWID();", c);
da.InsertCommand.Parameters.Add("_product_id", DbType.Int32,0,"product_id");
da.InsertCommand.Parameters.Add("_product_name", DbType.String, 0, "product_name");
da.InsertCommand.Parameters.Add("_abbrev", DbType.String, 0, "abbrev");
da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
da.UpdateCommand = b.GetUpdateCommand();
da.DeleteCommand = b.GetDeleteCommand();
da.Fill(dt);
bds.DataSource = dt;
grd.DataSource = bds;
}
private void uxUpdate_Click(object sender, EventArgs e)
{
da.Update(dt);
}
here's the sample table on SQLite:
CREATE TABLE [product] (
[product_id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[product_name] TEXT NOT NULL,
[abbrev] TEXT NOT NULL
)
[EDIT Nov 19, 2009 12:58 PM CN] Hmm... I guess my answer cannot be used, SQLCE does not allow multiple statements.
anyway, just use my answer when you use server-based MSSQL or if you use SQLite. or perhaps, encapsulate the two statements to a function that returns scope_identity(integer):
da.InsertCommand = new SQLiteCommand(
#"select insert_to_product(:_product_id, :_product_name, :_abbrev) as product_id", c);
da.InsertCommand.Parameters.Add("_product_id", DbType.Int32,0,"product_id");
da.InsertCommand.Parameters.Add("_product_name", DbType.String, 0, "product_name");
da.InsertCommand.Parameters.Add("_abbrev", DbType.String, 0, "abbrev");
da.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
note: just change SQLiteConnection and SQLiteDataAdapter to MSSQL ones, and change the LAST_INSERT_ROWID() to SCOPE_IDENTITY()
use RowUpdated (shall work on SQLCE and RDBMS that doesn't support multi-statements):
const string devMachine = #"Data Source=C:\_DEVELOPMENT\__.NET\dotNetSnippets\Mine\TestSqlite\test.s3db";
SQLiteConnection c = new SQLiteConnection(devMachine);
SQLiteDataAdapter da = new SQLiteDataAdapter();
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
da = new SQLiteDataAdapter("select product_id, product_name, abbrev from product", c);
var b = new SQLiteCommandBuilder(da);
da.InsertCommand = b.GetInsertCommand();
da.UpdateCommand = b.GetUpdateCommand();
da.DeleteCommand = b.GetDeleteCommand();
da.Fill(dt);
da.RowUpdated += da_RowUpdated;
bds.DataSource = dt;
grd.DataSource = bds;
}
void da_RowUpdated(object sender, System.Data.Common.RowUpdatedEventArgs e)
{
if (e.StatementType == StatementType.Insert)
{
int ident = (int)(long) new SQLiteCommand("select last_insert_rowid()", c).ExecuteScalar();
e.Row["product_id"] = ident;
}
}
private void uxUpdate_Click(object sender, EventArgs e)
{
da.Update(dt);
}
I haven't had a chance to use SQLiteConnection class but I do used SQLConnection and SQLCommand class. SqlCommand has a method ExecuteScalar that return the value of the first row and first column of your t-sql statement. You can use it to return the Auto-Identity column. Also, in SQL Server 2005 there is a keyword named OUTPUT you may also check it too.
I've come across this: all you need to do is set your autoincrement seed to -1 and have it "increment" by -1 too. This way all your datarows will have unique ids that DON'T map to anything in the real database. If you're saving your data with a DataAdapter, then after the save your datarow and any other rows with a datarelation pointing to that id will be updated

Invalid Object Name: Beginner using the AdventureWords db from a class

I'm trying to learn some C#.net. I'm just trying to expose the AdventureWorks database included in my C# class via a web interface. Here's the setup:
I've got a DropDownList in on my ASPX page with an id of tableNameDropDown. It gets populated on Page_Load like this:
protected void Page_Load(object sender, EventArgs e)
{
conn.Open();
String table_names_sql = "select Name from sysobjects where type='u' ORDER BY name";
SqlCommand cmd = new SqlCommand(table_names_sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
tableNameDropDown.Items.Add(reader[0].ToString());
}
conn.Close();
tableNameDropDown.AutoPostBack = true;
}
And that works just fine, I get a nice long list of the tables in the DB. When someone selects a table from the list, I want to display that table in a GridView control with an id of grid. This is what I've got:
protected void tableNameDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet dataSet = new DataSet();
String tableName = columnNameDropDown.SelectedItem.ToString();
String table_sql = String.Format("SELECT * FROM {0};", tableName);
SqlDataAdapter adapter = new SqlDataAdapter(table_sql, conn);
adapter.Fill(dataSet, tableName);
grid.DataSource = dataSet;
grid.DataMember = tableName;
}
When I debug the page, I get an error on the adapter.Fill(dataSet, tableName); line: SqlException: Inlvalid object name '{tableName}'.
The tables in the DB are the following:
dbo.AWBuildVersion
.... more dbo. tables
HumanResources.Department
HumanResources.Employee
.... more HumanResources tables
Person.Address
Person.AddressType
.... more Person tables
... Other prefixes are "Pdoduction, Purchasing, Sales"
There are probably ~50+ tables, and I get all their names (without the prefixes) into my DropDownList no problem, but I can't seem to query them.
Any ideas?
You've already answered yourself: you need to use also the prefix in the select statement you're executing, like
Select * From Person.Address
Beside that you should not use the sysobject tables, from SQL Server 2005 you have system views that helps you, so you can write a better statement to select tables:
select * From INFORMATION_SCHEMA.TABLES
Check also this article.
Regards
Massimo

Categories