In my database, there are two tables. Both have a username column. Table A is stores user information, and Table B is stores users' reservations. In my page, there will be a drop down menu that will retrieve BookingIDs for the currently logged in user. How should they be retrieved?
To help you understand, this should give you an idea of what I mean.
User X login,
If User X(Table A) = User X(Table B)
Then dropdown1 shows BookingID of User X from Table B.
I am sorry I don't provide any code because I really don't have any idea how to do it. Any answers are welcome. Thanks in advance.
This will list all BookingIDs of users that are in both tables:
select b.BookingID
from tableA a
inner join tableB b on a.username = b.username
Using SQL
SELECT
//TABLE_A.required_cols
//TABLE_B.required_cols
FROM
TABLE_A
JOIN TABLE_B ON TABLE_A.USER_ID = TABLE_B.USER_ID
Note:: Here I've assumed that both tables have a column called USER_ID that match for the same user, say X
Create a data table and then populate it create the table as follows:
Private Function CreateDataSource() As DataTable
'creates the columns for the datatable
Dim dt As New DataTable 'create new datatable
'add appropriate columns to the table
Dim colImage As New DataColumn("Field1", GetType(Boolean))
colImage.DefaultValue = bShowExtraInfo
dt.Columns.Add(colImage)
dt.Columns.Add("Field2", GetType(String))
Return dt 'return the table
End Function
and then use it in your code like so:
Dim dt As DataTable = CreateDataSource() 'create the data table
Dim dr As DataRow
For Each x In y 'cycle through x and add to table
dr("Field1") = tableAvalue
dr("Field2") = tableBvalue
dt.Rows.Add(dr)
End If
Next
gvEverything.DataSource = dt
gvEverything.DataBind()
Related
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.
Is it possible to add data from 2 tables in 1 datagridview.
And let table 2 add information that is equal with row[0].
The problem is Name is in another table as the needed information but ID's are matching.
Table 1 got : ID,Time and some other stuff.
Table 2 got : ID,Name.
So he needs to get the ID from row 0 and with Table 2 He needs to get the name WHERE ID is equal to Row 0
Yes it is possible to place two tables into the same datagridview. Run a query like this..
Select ID, name
From Table2
Where ID IN (
Select Top 1
ID
Table1
)
And then use this to fill a datatable, and set that as the data source of the datagridview.
Dont try to mess with DataGridView, Here is simple Solution based on ADO.NET
string select = "SELECT * FROM table1,table2 WHERE table1.ID=table2.ID";
MySqlConnection c = new MySqlConnection();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(select, c.con); //c.con is the connection string
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.ReadOnly = true;
dataGridView.DataSource = ds.tables[0];
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.
I am using ADO.NET. When I want to save data to table in SQL I need to retrieve columns information in this table. By information I mean Column max size (I want to get 10 from nvarchar(10) column) and NULL or NOT NULL.
I am using next code:
var selectFromCmd = SqlCommandFactory.CreateCommand("select top 0 * from [dbo]." + destTableName, SqlConnection, SqlTransaction);
var dataAdapter = new SqlDataAdapter(selectFromCmd);
var destinationTable = new DataTable();
dataAdapter.Fill(destinationTable);
Then I get DataColumn like so:
var column = destinationTable.Columns["MyColumn"]
But AllowDBNull is always true
and MaxLength is always -1 even if this column is string
So, how can I get the correct information about column properties in ADO.NET ?
I would rather use the sys cataloge views for this query. Something like this....
SELECT c.name ColumnName
,t.Name Datatype
,c.max_length MaxLength
,c.is_nullable
FROM sys.columns c
INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE c.object_id = object_id('Customers') --<-- your table name
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 = "%" &...