SQL Server can't execute - asp.net

There is a problem in my database when I execute this:
<asp:SqlDataSource ID="SqlDataSourceCombine" runat="server"
ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
DeleteCommand="DELETE FROM [Product] WHERE [ProductId] = #ProductId"
InsertCommand="INSERT INTO [Product] ([ProductName], [ProductDescription], [ProductCategory], [ProductBrand], [ProductPrice], [ProductQty], [ProductUploadDate]) VALUES (#ProductName, #ProductDescription, #ProductCategory, #ProductBrand, #ProductPrice, #ProductQty, #ProductUploadDate)"
SelectCommand="SELECT Product.*, ProductCategory.*, ProductBrand.*
FROM Product
INNER JOIN ProductCategory AS PC ON Product.ProductCategory = ProductCategory.CategoryId
INNER JOIN ProductBrand AS PB ON Product.ProductBrand = ProductBrand.BrandId"
UpdateCommand="UPDATE [Product] SET [ProductName] = #ProductName,
[ProductDescription] = #ProductDescription,
[ProductCategory] = #ProductCategory,
[ProductBrand] = #ProductBrand,
[ProductPrice] = #ProductPrice,
[ProductQty] = #ProductQty,
WHERE [ProductId] = #ProductId">
</asp:SqlDataSource>
I get this error:

You have multiple mistakes in your queries specified.
First your SELECT query uses Product just before last inner join which is wrong syntax. So, use the following query for select command.
SELECT query
SELECT product.*,
productcategory.*,
productbrand.*
FROM product
INNER JOIN productcategory
ON product.productcategory = productcategory.categoryid
INNER JOIN productbrand
ON product.productbrand = productbrand.brandid
Second, your UPDATE query has a comma just before WHERE which again is wrong syntax. Use query below for update command.
Update query
UPDATE [product]
SET [productname] = #ProductName,
[productdescription] = #ProductDescription,
[productcategory] = #ProductCategory,
[productbrand] = #ProductBrand,
[productprice] = #ProductPrice,
[productqty] = #ProductQty
WHERE [productid] = #ProductId
Your final markup should be as below.
SqlDataSource with corrected queries
<asp:SqlDataSource ID="SqlDataSourceCombine" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
DeleteCommand="DELETE FROM [Product] WHERE [ProductId] = #ProductId"
InsertCommand="INSERT INTO [Product] ([ProductName], [ProductDescription], [ProductCategory], [ProductBrand], [ProductPrice], [ProductQty], [ProductUploadDate]) VALUES (#ProductName,
#ProductDescription, #ProductCategory, #ProductBrand, #ProductPrice, #ProductQty, #ProductUploadDate)"
SelectCommand="SELECT product.*, productcategory.*,
productbrand.* FROM product
INNER JOIN productcategory
ON product.productcategory = productcategory.categoryid
INNER JOIN productbrand
ON product.productbrand = productbrand.brandid "
UpdateCommand="UPDATE [product]
SET [productname] = #ProductName,
[productdescription] = #ProductDescription,
[productcategory] = #ProductCategory,
[productbrand] = #ProductBrand,
[productprice] = #ProductPrice,
[productqty] = #ProductQty
WHERE [productid] = #ProductId ">
</asp:SqlDataSource>

Related

Why that SQLite command does not work in Unity?

If this command is run in Sqlite, it works fine. However, if you try from Unity, an error occurs.
'''
IDbCommand command = dbConnection.CreateCommand();
command.CommandText = #"
UPDATE AnotherTable SET V1 = (SELECT Intelligence from Table1
where AnotherTable.id in (
select AnotherTable.id from AnotherTable
inner join Table1 on AnotherTable.id = Table1.Id
WHERE AnotherTable.V2 = 0 and AnotherTable.V1 = "") and
Table1.Id = AnotherTable.Id) where id in(
select AnotherTable.id from AnotherTable
inner join Table1 on AnotherTable.id = Table1.Id
WHERE AnotherTable.V2 = 0 and AnotherTable.V1 = "");
";
command.ExecuteNonQuery();
command.Dispose();
base.SetCubicsGreenField();
'''
error text:
'''
SQL logic error
near ";": syntax error; at System.Data.SQLite.SQLite3.Prepare (System.Data.SQLite.SQLiteConnection cnn, System.String strSql, System.Data.SQLite.SQLiteStatement previous, System.UInt32 timeoutMS, System.String& strRemain) [0x003d4] in ..
'''
Instead of empty quotes, use:
length(let) = 0

left join with external query in query-builder

I need to create that query in query builder
SELECT * FROM table1 LEFT JOIN table2 ON (table1.parent_id = table2.id OR table1.id = table2.id) WHERE table2.id IS NULL;
I already've got
$er->createQueryBuilder('p')
->leftJoin('Bundle2:table2', 'n')
->where('p.parent = n.id')
->andWhere('p.id = n.id');
but don't know how to add external WHERE to query ?
Try this:
return $this->createQueryBuilder('t1')
->leftJoin('t1.table2', 't2')
->where('t1.parent = t2.id OR t1.id = t2.id')
->andWhere('t2.id IS NULL')
->getQuery()
->getResult();

SqlDataSource SelectCommand contains null values

My SQL code that works running on the server.
SELECT
[LINE_NO], [CUST_ORDER_ID], [PART_ID],
[CUSTOMER_PART_ID], [MISC_REFERENCE], [PROMISE_DATE],
[PROMISE_DEL_DATE]
FROM
[CUST_ORDER_LINE]
WHERE
([CUST_ORDER_ID] = '33742-1'
AND [PROMISE_DATE] IS NULL
AND [PROMISE_DEL_DATE] IS NULL)
My asp.net code.
SelectCommand = "SELECT [LINE_NO], [CUST_ORDER_ID], [PART_ID], [CUSTOMER_PART_ID], [MISC_REFERENCE], [PROMISE_DATE], [PROMISE_DEL_DATE] FROM [CUST_ORDER_LINE] WHERE ([CUST_ORDER_ID] = ? AND [PROMISE_DATE] = ? AND [PROMISE_DEL_DATE] = ?)"
I'm using 3 query string parameters. The promise date and promise delivery date can be null. But when those values are null it returns no records.
How can I program this to change the SQL to 'is null' instead of = ''.
I think this is what you want:
Param 1 = OrderID
Param 2/3 are both Promise Date
Param 4/5 are both Promise Delivery Date
Code:
SelectCommand = "SELECT [LINE_NO], [CUST_ORDER_ID], [PART_ID], [CUSTOMER_PART_ID], [MISC_REFERENCE], [PROMISE_DATE], [PROMISE_DEL_DATE] FROM [CUST_ORDER_LINE] WHERE [CUST_ORDER_ID] = ? AND (? IS NULL OR [PROMISE_DATE] = ?) AND (? IS NULL OR [PROMISE_DEL_DATE] = ?)"
The SQL would be:
DECLARE #P1 INT, #P2 DATETIME, #P3 DATETIME
SELECT [LINE_NO], [CUST_ORDER_ID], [PART_ID], [CUSTOMER_PART_ID], [MISC_REFERENCE], [PROMISE_DATE], [PROMISE_DEL_DATE]
FROM [CUST_ORDER_LINE]
WHERE [CUST_ORDER_ID] = #P1
AND (#P2 IS NULL OR [PROMISE_DATE] = #P2)
AND (#P3 IS NULL OR [PROMISE_DEL_DATE] = #P3)

build a select query as a gridview datasource asp.net

I use an asp.net gridview depend on controls to search
day : - month : - year :
in database table I got an startdate field contain the whole date :
I try this query :
SELECT id, shift_id, name_of_shift, person_in_shift, starttime_in_shift, endtime_in_shift, table_id, startdate, enddate, point_id
FROM sarcshifttable
WHERE (id IN
(SELECT MIN(id) AS Expr1
FROM sarcshifttable AS sarcshifttable_1
GROUP BY table_id)) AND (DATEPART(year, startdate) + '' LIKE #year) AND (DATEPART(day, startdate) + '' LIKE #day) AND (DATEPART(month, startdate) + '' LIKE #month)
the controls :
<asp:TextBox ID="day_search" CssClass="textfield NjmeDine_Integer" Width="40px" runat="server"></asp:TextBox>
<asp:TextBox ID="month_search" CssClass="textfield NjmeDine_Integer" Width="40px" runat="server"></asp:TextBox>
<asp:TextBox ID="year_search" CssClass="textfield NjmeDine_Integer" Width="80px" runat="server"></asp:TextBox>
the select parameter :
<SelectParameters>
<asp:ControlParameter ControlID="year_search" DefaultValue="%" Name="year" PropertyName="Text" />
<asp:ControlParameter ControlID="month_search" DefaultValue="%" Name="month" PropertyName="Text" />
<asp:ControlParameter ControlID="day_search" DefaultValue="%" Name="day" PropertyName="Text" />
</SelectParameters>
the startdate in this format : 2014-03-01
note :
when I try it in database with a static value it's work perfectly
SELECT id, shift_id, name_of_shift, person_in_shift, starttime_in_shift, endtime_in_shift, table_id, startdate, enddate, point_id
FROM sarcshifttable
WHERE (DATEPART(month, startdate) + '' LIKE 03) AND (id IN
(SELECT MIN(id) AS Expr1
FROM sarcshifttable AS sarcshifttable_1
GROUP BY table_id)) AND (DATEPART(year, startdate) + '' LIKE 2014) AND (DATEPART(day, startdate) + '' LIKE 01) AND (DATEPART(month, startdate) + '' LIKE 03)
the answer is :
SELECT id, shift_id, name_of_shift, person_in_shift, starttime_in_shift, endtime_in_shift,
SELECT id, shift_id, name_of_shift, person_in_shift, starttime_in_shift, endtime_in_shift, table_id, startdate, enddate, point_id
FROM sarcshifttable
WHERE (DATEPART(month, startdate) + '' LIKE 03) AND (id IN
(SELECT MIN(id) AS Expr1
FROM sarcshifttable AS sarcshifttable_1
GROUP BY table_id)) AND ((DATEPART(year, startdate) + '' LIKE '%'+#year+'%') AND (DATEPART(day, startdate) + '' LIKE '%'+#day+'%') AND (DATEPART(month, startdate) + '' LIKE '%'+#month+'%')

Sql select statement selecting 4 value from same table

Hello everyone I have created two select statements but I want to merge them together to give one select statement that display only 4 record can someone help please?
SELECT
DATENAME(month, BID.Date) AS Years,
COUNT(DATENAME(Month, BID.Date)) AS Total
FROM
Auction
INNER JOIN
BID ON Auction.AuctionID = BID.AuctionID
INNER JOIN
Item ON Auction.ItemID = Item.ItemID
WHERE
(Auction.Status = 'Expired')
AND (Item.SellerID = #seller)
AND (BID.Status = 'Won')
GROUP BY
DATENAME(month, BID.Date)
SELECT
DATENAME(month, BID.Date) AS Years1,
COUNT(DATENAME(Month, BID.Date)) AS Total1
FROM
Auction
INNER JOIN
BID ON Auction.AuctionID = BID.AuctionID
INNER JOIN
Item ON Auction.ItemID = Item.ItemID
WHERE
(Auction.Status = 'UnSold')
AND (Item.SellerID = #seller)
GROUP BY
DATENAME(month, BID.Date)
SELECT
DATENAME(month, BID.Date) AS Years
,COUNT(CASE WHEN Auction.Status = 'Expired'
AND Item.SellerID = #seller
AND BID.Status = 'Won'
THEN DATENAME(Month, BID.Date) ELSE NULL END) AS Total1
,COUNT(CASE WHEN Auction.Status = 'UnSold'
AND Item.SellerID = #seller
THEN DATENAME(Month, BID.Date) ELSE NULL END) AS Total2
FROM Auction INNER JOIN BID
ON Auction.AuctionID = BID.AuctionID
INNER JOIN Item
ON Auction.ItemID = Item.ItemID
GROUP BY DATENAME(month, BID.Date)

Resources