In Data table first column displaying with tablename.columnname in c# - c#

When i am running query in data base and fill data table with load method first column name coming with table name.column name(Employee.Name,sal,location) rest of the column name coming only column names present in the database.Why table name appending first column name while displaying in data table structure please help me in this regards.
DataTable table = new DataTable();
try
{
using (IDbCommand DbCommand = dbConnection.CreateCommand())
{
DbCommand.CommandText = query;
IDataReader reader = DbCommand.ExecuteReader();
table.Load(reader);
}
}
Sample query Select Employee.Numnber, Employee.salary, Employee.city,dept.deptId from Employee inner join dept where deptiId=EmployeeDeptId
The actual query is :
Select
AP_LINE.LINE_NO, AP_LINE.PRODUCT, AP_LINE.QTY, AP_LINE.REQUESTED_DATE,
AP_LINE.LIST_PRICE, AP_LINE.CONS_NET_MULT,AP_LINE.NETADDERS,
AP_LINE.CONS_NET_PRICE, AP_LINE.Details,TDP_JSP.ID, TDP_JSP.LINE_NO,
TDP_JSP_AppInfo.Id
From AP_LINE, CONFIG, TDP_JSP
inner join TDP_JSP_AppInfo on TDP_JSP.ID= TDP_JSP_AppInfo.ID
where AP_LINE.LINE_NO = TDP_JSP.LINE_NO ;
Out put in data table as coming below
Ap_Line.Line_No,Product,QTY,Requested Date...

There are two LINE_NO columns in the query, AP_LINE.LINE_NO and TDP_JSP.LINE_NO. Columns must have unique names, so DataTable.Load used the two-part name wherever needed. It that wasn't possible it would start appending indexes or even generate names like Column0, Column1 etc.
In this case though, one of those columns isn't needed. The two tables are join those columns. WHERE AP_LINE.LINE_NO = TDP_JSP.LINE_NO is the old-style, frowned-upon join syntax. Those two columns will always have the same data so one of them can be removed.

Related

SQLite Inner Join Issue

I have two tables with some odd join behavior. Here is a brief schema with data:
CREATE TABLE object(vnum INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE object_affect(vnum INTEGER, apply_id INTEGER, modifier INTEGER);
object
vnum name
10404 Test Item
object_affect
vnum apply_id modifier
10404 1 4
10404 5 2
10404 12 6
If I run the below query I should have 3 rows returned however it only returns 1 row.
select * from object o
inner join object_affect oa on oa.vnum = o.vnum
where o.vnum = 10404
If I change the query to this (change * to oa.vnum).. it return 3 rows:
select oa.vnum from object o
inner join object_affect oa on oa.vnum = o.vnum
where o.vnum = 10404
Also, if I remove the "PRIMARY KEY" flag in the create table statement from the vnum field on object table the first query that only returned one 1 returns all 3 joined rows correctly.
What am I missing that causes the first query to not return 3 rows of joined data?
I ran into this again 3.5 years later. I wanted to share the solution. The idea in this case is that the DataTable is used to display the results of a query on some kind of control, a DataGrid, etc. As a result, it doesn't need the constraints the DataTable tries to infer from the database about unique keys, etc. There's no good way to disable the constraints on the DataTable that I've found by itself but you can couple it with a DataSet and do just that. This snippet (assuming the SqliteCommand is already setup) works:
using (var dr = cmd.ExecuteReader())
{
var dt = new DataTable();
using (var ds = new DataSet() { EnforceConstraints = false })
{
ds.Tables.Add(dt);
dt.BeginLoadData();
dt.Load(dr);
dt.EndLoadData();
ds.Tables.Remove(dt);
}
}
Basically we temporarly use the DataSet and then discard it (it is important to remove it from the DataSet so the DataSet can Dispose and be garbage collected). Then you can keep the DataTable and persist it to bind it to a control.

How to filter Data table

I want to filter data table based on some condition .
I have a data table like that
Tabel A
MobileNo Email
9999999999 test#test1.com
8888888888 test#test2.com
9999999999 test#test5.com
7777777777 test#test6.com
I want to get distinct value based on mobile also I need data that not exits in distinct table but table A have
Like
Distinct Table Ignore Table
MobileNo Email MobileNo Email
9999999999 test#test1.com 9999999999 test#test5.com
8888888888 test#test2.com
7777777777 test#test6.com
I have also tried googling but that are not very much understandable.
Thanks in Advance
You can use the select method on DataTable
Dim dtFiltered As New DataTable()
dtFiltered = dtAllData.DefaultView.ToTable(True, "MobileNo")
dtAllData is the DataTable containing all records
dtFiltered contains only Distinct Records
For more details check MSDN article
UPDATE based on your comment
In that case you need to use except as in
datatable-comparison-using-linq-except-intersect-union
From all records extract distinct records to second DataTable and then use Except to compare the first and second DataTable
Thanks for all your help
Finally I got the solution
Dim ValidData = (From row In tbValid.AsEnumerable()
Let Mobile = row.Field(Of String)("Mobile")
Group row By Mobile Into DupMobile = Group
Select DupMobile.First).ToArray
Dim Ignoredata = tbValid.AsEnumerable().Except(ValidData.AsEnumerable(), DataRowComparer.Default).ToArray
From your example all you have done is order your mobile no. in desending order which means you could then create a SQL to say something like: ORDER BY MobileNo DESC at the end of the statement. At least it will display the same way
Or are you meaning:
Dim dv As DataView = New DataView(DataTable.DataView)
dv.RowFilter = "%" &...

How do I use LINQ to update a datatable with a SqlDataReader?

I am trying to merge data from two separate queries using C#. The data is located on separate servers or I would just combine the queries. I want to update the data in one of the columns of the first data set with the data in one of the columns of the second data set, joining on a different column.
Here is what I have so far:
ds.Tables[3].Columns[2].ReadOnly = false;
List<object> table = new List<object>();
table = ds.Tables[3].AsEnumerable().Select(r => r[2] = reader.AsEnumerable().Where(s => r[3] == s[0])).ToList();
The ToList() is just for debugging. To summarize, ds.Tables[3].Rows[2] is the column I want to update. ds.Tables[3].Rows[3] contains the key I want to join to.
In the reader, the first column contains the matching key to ds.Tables[3].Rows[3] and the second column contains the data with which I want to update ds.Tables[3].Rows[2].
The error I keep getting is
Unable to cast object of type 'WhereEnumerableIterator1[System.Data.IDataRecord]' to type 'System.IConvertible'.Couldn't store <System.Linq.Enumerable+WhereEnumerableIterator1[System.Data.IDataRecord]> in Quoting Dealers Column. Expected type is Int32.
Where am I going wrong with my LINQ?
EDIT:
I updated the line where the updating is happening
table = ds.Tables[3].AsEnumerable().Select(r => r[2] = reader.AsEnumerable().First(s => r[3] == s[0])[1]).ToList();
but now I keep getting
Sequence contains no matching element
For the record, the sequence does contain a matching element.
You can use the following sample to achieve the join and update operation. Let's suppose there are two Datatables:
tbl1:
tbl2:
Joining two tables and updating the value of column "name1" of tbl1 from column "name2" of tbl2.
public DataTable JoinAndUpdate(DataTable tbl1, DataTable tbl2)
{
// for demo purpose I have created a clone of tbl1.
// you can define a custom schema, if needed.
DataTable dtResult = tbl1.Clone();
var result = from dataRows1 in tbl1.AsEnumerable()
join dataRows2 in tbl2.AsEnumerable()
on dataRows1.Field<int>("ID") equals dataRows2.Field<int>("ID") into lj
from reader in lj
select new object[]
{
dataRows1.Field<int>("ID"), // ID from table 1
reader.Field<string>("name2"), // Updated column value from table 2
dataRows1.Field<int>("age")
// .. here comes the rest of the fields from table 1.
};
// Load the results in the table
result.ToList().ForEach(row => dtResult.LoadDataRow(row, false));
return dtResult;
}
Here's the result:
After considering what #DStanley said about LINQ, I abandoned it and went with a foreach statement. See code below:
ds.Tables[3].Columns[2].ReadOnly = false;
while (reader.Read())
{
foreach (DataRow item in ds.Tables[3].Rows)
{
if ((Guid)item[3] == reader.GetGuid(0))
{
item[2] = reader.GetInt32(1);
}
}
}

How to Update one table by another table datas using storedprocedure?

I have a Table named Dummywith marks and student id (3 mark fields are there). I have another one table Applicantdetails this table also contains mark fields. what I want to do is I want to updates Dummy tables marks to Applicantdetails table's marks as per student id. I want to do this by mssql Storedprocedure. Any way to achieve it. If we write in code wise it should be like this
qry="select Applicantid,mark1,mark2,mark3 from Dummy"
//saved result to Datatable dt
foreaach(DataRow. rows in dt.rows)
{
string id=Convert.ToString(row["ApplicantID"].tostring();
string mark1=Convert.ToString(row["ApplicantID"].tostring();
string mark2=Convert.ToString(row["ApplicantID"].tostring();
string mark3=Convert.ToString(row["ApplicantID"].tostring();
qry="update Applicantdetails set Mark1=mark1,Mark2=Mark2,Mark3=Mark3
where ApplicantID=id";
}
This format I want to bring in storedprocedure..Please help me
Your Stored procedure would receive #applicationid ? Otherwise it would update all, Create a stored procedure with the following SQL
UPDATE A
SET Mark1= d.mark1, Mark2 = d.mark2, Mark3= d.mark3
FROM ApplicationDetail A
JOIN Dummy d on d.Applicationid = A.Applicationid

Inner Join a C# datatable and Sql Datatable using linq

I know this is obviously a repeated question to ask but I am unable to figure out the issue, as I am new to LINQ.
Basically I have to matchup for duplicate entry of data while adding multiple records at a time. So, I have a Table in my database that has few rows and then I create DataTable dynamically which is clone(in terms of structure) of that table. Now dtDup is the Database Table, returned as dataset/datatable from a select query, and dupVals is the dynamic clone that is to be cross checked for duplicates
var CommnRows =
from dbA in dtDup.AsEnumerable()
join appB in dupVals.AsEnumerable() on
new {
MonthID = dbA.Field<int>("MonthID"),
UserID = dbA.Field<int?>("UserID"), //nullable int
IsActive = dbA.Field<bool?>("IsActive"), //nullable bit
Gender = dbA.Field<String>("Gender").ToString().ToUpper()
}
equals
new {
MonthID = appB.Field<int>("MonthID"),
UserID = appB.Field<int?>("UserID"),
IsActive = appB.Field<bool?>("IsActive")
Gender = appB.Field<String>("Gender").ToString().ToUpper()
}
select dbA;
So, in case I have some rows returned then (I assume, that above join is correct inner join) this means that there are duplicate rows.
But I am getting an error:
Object reference not set to an instance of an object
at new after equals
I found the issue. I was trying to change the type of one of my column that was a string and at first I did not included in the question (now updated), but when I tried debugging it line by line, I found that it was breaking while near Gender. So, just removed the ToString().ToUpper() from that area and it worked.

Categories