SqlDataSource not Working - asp.net

Select pmtblempreg.Id
,pmtblempreg.Name
,pmtblempreg.UserName
,pmtbldesignation.Designation
, pmtblempreg.skypeid
,pmtblempreg.EmailId
From pmtbldesignation
Right join pmtblempreg ON
pmtbldesignation.Id = pmtblempreg.DesignationId
This query works fine in oracle database. But when executing using sqlDataSource for populating a gridview an error is showing that invalid table name
when i execute it through the query builder of sqldataSource the query changes to
SELECT PMTBLEMPREG.ID
,PMTBLEMPREG.NAME
,PMTBLEMPREG.USERNAME
,PMTBLDESIGNATION.DESIGNATION
,PMTBLEMPREG.SKYPEID
,PMTBLEMPREG.EMAILID
FROM { **oj PMTBLDESIGNATION**
RIGHT OUTER JOIN PMTBLEMPREG ON
PMTBLDESIGNATION.ID = PMTBLEMPREG.DESIGNATIONID
}
an oj is automatically created along with the table name.
Is that the problem??

Try like this
Just Use this code in CodeBehid
Dim SQLStatement As String = "Select pmtblempreg.Id,pmtblempreg.Name,pmtblempreg.UserName ,pmtbldesignation.Designation, pmtblempreg.skypeid,pmtblempreg.EmailId From pmtbldesignation
Right join pmtblempreg ON pmtbldesignation.Id = pmtblempreg.DesignationId"
sqldatsrc.ConnectionString = ConnectionString
sqldatsrc.SelectCommand = SQLStatement
Or Paste the query in SQLDatasource Select Command as
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=xx;User ID=xx;Password=xxx" ProviderName="xx" SelectCommand="Select pmtblempreg.Id,pmtblempreg.Name,pmtblempreg.UserName ,pmtbldesignation.Designation, pmtblempreg.skypeid,pmtblempreg.EmailId From pmtbldesignation
Right join pmtblempreg ON pmtbldesignation.Id = pmtblempreg.DesignationId"></asp:SqlDataSource>

SELECT PE.ID
,PE.NAME
,PE.USERNAME
,PD.DESIGNATION
,PE.SKYPEID
,PE.EMAILID
FROM PMTBLDESIGNATION PD
RIGHT OUTER JOIN PMTBLEMPREG PE ON
PD.ID = PE.DESIGNATIONID
where PD is the Table name alias for PMTBLDESIGNATION and PE is the Table name alias for PMTBLEMPREG
Updated:
By default if you execute the query it checks the table in the Master Database
Instead before executing query Write this code
Use Your_database_name
The database name is where the tables are placed.
Try this
Hope this Helps

Related

Giving alias in JOIN command in sqldatasource

I am using JOIN command to connect two tables in SqlDataSource.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CS %>"
SelectCommand="SELECT UserName as UN, AboutMe, WebPage, Email FROM Users JOIN ProfileImages ON ProfileImages.UserName = Users.UN WHERE (UN = #UserName)" >
</asp:SqlDataSource>
In MS SQL Server Management Studio this command is ok. But in the SqlDataSource it return errors - Invalid column name 'UN', Ambiguous column name 'UserName'
How do I alias this command right in SqlDataSource to not return these errors?
Your query is a bit wrong
You used alias i a wrong way
First:
You can use alias in the following sections:
Fields. Example Username as UN
Tables names: UserTable as US
In the order by section example: Order BY UN Desc
This because the sql runs the query operations in the following sequence:
FROM clause
WHERE clause
GROUP BY clause
HAVING clause
SELECT clause
ORDER BY clause
So because the FROM clause runs first the sql doesn't know about the alias in the SELECT clause
Take a look on a correct query
SELECT
UserName as UN,
AboutMe,
WebPage,
Email
FROM Users
JOIN ProfileImages ON ProfileImages.UserName = Users.UserName
WHERE (Users.UserName = #UserName)
I'm not an expert at SQL, but if both ProfileImages and Users contain the column 'UserName', then in your select columns you need to specify the table from which the UserName is pulled. So something like:
SELECT Users.UserName as UN, Users.AboutMe, Users.WebPage, Users.Email
FROM Users
JOIN ProfileImages ON ProfileImages.UserName = Users.UserName
WHERE (Users.UserName = #UserName)
You cannot use the column alias in the where clause.
SELECT UserName as UN, AboutMe, WebPage, Email
FROM Users usersAlias JOIN ProfileImages piAlias ON piAlias.UserName = usersAlias.UserName
WHERE (usersAlias.UserName = #UserName)
It makes sense when you look at the order of execution:
http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/
Since the order of execution is:
These phases and their orders are given as follows:
FROM
ON
OUTER
WHERE
GROUP BY
CUBE | ROLLUP
HAVING
SELECT
DISTINCT
10 ORDER BY
TOP
When these are being processed (the "from" (#1) and "on" (#2) and "where" (#4))..they have no knowledge about the "select" (#8) (aka, the alias name for UN), the column alias in the select clause cannot be used in the WHERE and FROM (and ON) clauses.

asp sqldatasource text

I need to have a sqldatasource with the following sql in it.
if #filter = 'departments'
begin
SELECT ISNULL(DocTitle,'') as Name, DocNumber as id, DocUrl+DocName AS link,LastModBy,LastModDate, IsLink
FROM cmc.CMC_Docs d
INNER JOIN CMC.CMC_Doc_Locations as l on l.FamilyID = d.FamilyID
INNER JOIN CMC.CMC_DocFamilies df on df.FamilyID = d.FamilyId
WHERE IsEnabled=1
AND ISNULL(DocName,'') <> ''
AND d.FamilyID IN #dep
ORDER by DocTitle
end
where #dep is something like (2,3)
However when I try to test the query I get an error saying incorrect syntax near #dep.
Any ideas how I need to write this inside of the datasource in order for it to work?
Thanks,
do you need to put this in ()?
ex: select * from product where productid in (1,2,3) works
ex: select * from product where productid in 1,2,3 - does not work

display columns from different tables during data bind

How to bind data from two tables.
tbl_user
first name userid
tbl_usermessage
userid timereceived msgid
How to display username and timereceived in datagrid
SELECT TimeReceived, FirstName FROM tbl_usermessage INNER JOIN tbl_user on tbl_usermessage.tbl_user_UserID = tbl_user.UserID WHERE tbl_message_MsgID = #Value1";
This is what I am trying i am getting syntax error. here Time received is from tbl_usermessage and firstname is from tbl_User and both table has userid
How about joining both tables on your sql query?
You need to connect a SqlDataSource to your DataGrid by setting the SqlDataSource's DataSourceID property to the SqlDataSourceID. Set the SqlDataSource's SelectCommand property to the SQL required to get the items:
SelectCommand="SELECT tableone.username, tableone.userid, tabletwo.userid, tabletwo.timereceived
FROM tableone INNER JOIN tabletwo ON tableone.userid=tabletwo.userid"
And also set the ConnectionString property:
ConnectionString="<%$ ConnectionStrings:MyDatabaseConnectionString %>" >
You need to retrieve the data from your database with a SQL Query which JOINs the two tables on a column that is common to both:
Something along these lines:
SELECT
userId, username, timereceived
FROM
Table1 INNER JOIN Table2 ON Table1.userId = Table2.UserID
Here's an example for your reference.

DataGrid view in asp.net is not displaying data

I want to dispaly column in datagrid view using custom query statement
but i want to send value to parametrized data in query
how can i do this ?
my code is below
select
c.customer_id,
c.customer_first_name,
c.customer_last_name,
c.customer_address,
c.account_number,
c.account_type,
a.account_balance,
t.transfer_amount_balance,
t.samebank_transfer_id,
t.account_number_same_bank,
t.transferdatetime
FROM customer_details c join account_details a on c.account_number = a.account_number
join transactions_details t on a.account_number = t.account_number where
c.customer_id = 'cus0010' and a.account_number = 'acc0010'
this above code working properly in sql server 2005
but the code below which is modified as per asp.net page for grid view is not showing
any result
select
c.customer_id,
c.customer_first_name,
c.customer_last_name,
c.customer_address,
c.account_number,
c.account_type,
a.account_balance,
t.transfer_amount_balance,
t.samebank_transfer_id,
t.account_number_same_bank,
t.transferdatetime
FROM customer_details c join account_details a on c.account_number = a.account_number
join transactions_details t on a.account_number = t.account_number where
c.customer_id = 'Label1.Text' and a.account_number = 'Label2.Text'
the above is placed in my custom sql query section it
is triggered by button click in my asp page or any other
idea to display it will be welcomed
Use:
string.Format("c.customer_id = '{0}' and a.account_number = '{1}'", Label1.Text, Label2.Text);
Consider this query:
string query = "insert into TestTable (Column1, Column2) values (#p1, #p2)";
p1 & p2 are parameters, in order to set the value for the parameters you need to use:
queryParameters[0] = new SqlCeParameter("p1", SqlDbType.NVarChar);
queryParameters[0].Value = Label1.Text;
queryParameters[1] = new SqlCeParameter("p2", SqlDbType.NVarChar);
queryParameters[1].Value = Label2.Text;
SqlCeCommand command = new SqlCeCommand(query);
command.Parameters.AddRange(queryParameters);
When the wizard is generating the query you need to use place holders/parameters for customer_ID and account_number and set their values by using parameters.
Edit:
In order to make the wizard create a parameter to use in the query, add a ? in the filter column in the query builder wizard.
Well, I may misunderstand, but...you are not actually sending the string 'Label1.Text' I guess? You should send the value of the textbox, something like this (if you are building the SQL as a string?):
...[SQL]... + "c.customer_id = '"+ Label1.Text + "'" ...[rest of the SQL]

NHibernate Collection Left Outer Join Where Clause Issue

It seems that when using the following NHibernate query, I do not get a root entity when the left outer join has no records.
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(Expression.Not(Expression.Eq("Property", value)));
The SQL that I am trying to generate is:
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
WHERE Property <> value
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
Notice that the where clause is inside the left join's select statement. That way if there arent any maching sub records, we still get a top level record. It seems like NHibernate is producing the following SQL.
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
WHERE Sub.Property <> value
Is there anyway to achieve that first piece of SQL? I have already tried:
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(
Restrictions.Disjunction()
.Add(Expression.IsNull("Property"))
.Add(Expression.Not(Expression.Eq("Property", value)));
I am looking for a solution using the Criteria API.
Try this:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
with subt.Property <> :property";
Or perhaps:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
where subt.ForeignKey = bt.PrimaryKey
and subt.Property <> :property";
Finally:
var result = session.CreateQuery(hql)
.SetParameter("property", "whateverValue")
.List<BaseTable>();
I don't use nHibernate but I think this is the SQL you need to generate:
SELECT *
FROM BaseTable
LEFT JOIN SubTable sub
ON Sub.ForeignKey = BaseTable.PrimaryKey and sub.Property <> value
What you want isn;t a where clasue but an additional condition on the join. Hope that helps.

Resources