Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Wednesday, March 21, 2012

Errors when I use the "Delete" function

I've been trying to build a data table that allows a client to add, edit and delete his itinerary. I've been using the products table in Northwind to get up to speed on this functions and everything works fine - (ie-connects to the database, shows the data I need, allows me to edit the data) but when I try to use the delete function, I keep getting the error below.

DELETE statement conflicted with COLUMN REFERENCE constraint 'FK_Order_Details_Products'. The conflict occurred in database 'Northwind', table 'Order Details', column 'ProductID'.
The statement has been terminated.

Can anybody help me with this problem?

thanx

Code used is below:

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Gigs.aspx.cs"Inherits="Gigs" %>

<!

DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

headrunat="server"><title>Untitled Page</title>

</

head>

<

body><formid="form1"runat="server"><div><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @.ProductID"InsertCommand="INSERT INTO [Products] ([ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued]) VALUES (@.ProductName, @.SupplierID, @.CategoryID, @.QuantityPerUnit, @.UnitPrice, @.UnitsInStock, @.UnitsOnOrder, @.ReorderLevel, @.Discontinued)"SelectCommand="SELECT * FROM [Products]"UpdateCommand="UPDATE [Products] SET [ProductName] = @.ProductName, [SupplierID] = @.SupplierID, [CategoryID] = @.CategoryID, [QuantityPerUnit] = @.QuantityPerUnit, [UnitPrice] = @.UnitPrice, [UnitsInStock] = @.UnitsInStock, [UnitsOnOrder] = @.UnitsOnOrder, [ReorderLevel] = @.ReorderLevel, [Discontinued] = @.Discontinued WHERE [ProductID] = @.ProductID"><DeleteParameters><asp:ParameterName="ProductID"Type="Int32"/></DeleteParameters><UpdateParameters><asp:ParameterName="ProductName"Type="String"/><asp:ParameterName="SupplierID"Type="Int32"/><asp:ParameterName="CategoryID"Type="Int32"/><asp:ParameterName="QuantityPerUnit"Type="String"/><asp:ParameterName="UnitPrice"Type="Decimal"/><asp:ParameterName="UnitsInStock"Type="Int16"/><asp:ParameterName="UnitsOnOrder"Type="Int16"/><asp:ParameterName="ReorderLevel"Type="Int16"/><asp:ParameterName="Discontinued"Type="Boolean"/><asp:ParameterName="ProductID"Type="Int32"/></UpdateParameters><InsertParameters><asp:ParameterName="ProductName"Type="String"/><asp:ParameterName="SupplierID"Type="Int32"/><asp:ParameterName="CategoryID"Type="Int32"/><asp:ParameterName="QuantityPerUnit"Type="String"/><asp:ParameterName="UnitPrice"Type="Decimal"/><asp:ParameterName="UnitsInStock"Type="Int16"/><asp:ParameterName="UnitsOnOrder"Type="Int16"/><asp:ParameterName="ReorderLevel"Type="Int16"/><asp:ParameterName="Discontinued"Type="Boolean"/></InsertParameters></asp:SqlDataSource>

</div><asp:GridViewID="GridView1"runat="server"AllowPaging="True"AutoGenerateColumns="False"CellPadding="4"DataKeyNames="ProductID"DataSourceID="SqlDataSource1"ForeColor="#333333"GridLines="None"><FooterStyleBackColor="#990000"Font-Bold="True"ForeColor="White"/><Columns><asp:CommandFieldShowDeleteButton="True"ShowEditButton="True"/><asp:BoundFieldDataField="ProductID"HeaderText="ProductID"InsertVisible="False"ReadOnly="True"SortExpression="ProductID"/><asp:BoundFieldDataField="ProductName"HeaderText="ProductName"SortExpression="ProductName"/><asp:BoundFieldDataField="SupplierID"HeaderText="SupplierID"SortExpression="SupplierID"/><asp:BoundFieldDataField="CategoryID"HeaderText="CategoryID"SortExpression="CategoryID"/><asp:BoundFieldDataField="QuantityPerUnit"HeaderText="QuantityPerUnit"SortExpression="QuantityPerUnit"/><asp:BoundFieldDataField="UnitPrice"HeaderText="UnitPrice"SortExpression="UnitPrice"/><asp:BoundFieldDataField="UnitsInStock"HeaderText="UnitsInStock"SortExpression="UnitsInStock"/><asp:BoundFieldDataField="UnitsOnOrder"HeaderText="UnitsOnOrder"SortExpression="UnitsOnOrder"/><asp:BoundFieldDataField="ReorderLevel"HeaderText="ReorderLevel"SortExpression="ReorderLevel"/><asp:CheckBoxFieldDataField="Discontinued"HeaderText="Discontinued"SortExpression="Discontinued"/></Columns><RowStyleBackColor="#FFFBD6"ForeColor="#333333"/><SelectedRowStyleBackColor="#FFCC66"Font-Bold="True"ForeColor="Navy"/><PagerStyleBackColor="#FFCC66"ForeColor="#333333"HorizontalAlign="Center"/><HeaderStyleBackColor="#990000"Font-Bold="True"ForeColor="White"/><AlternatingRowStyleBackColor="White"/></asp:GridView></form>

</

body>

</

html>

aspx.cs code used below:

using

System;

using

System.Data;

using

System.Configuration;

using

System.Collections;

using

System.Web;

using

System.Web.Security;

using

System.Web.UI;

using

System.Web.UI.WebControls;

using

System.Web.UI.WebControls.WebParts;

using

System.Web.UI.HtmlControls;

public

partialclassGigs : System.Web.UI.Page

{

protectedvoid Page_Load(object sender,EventArgs e)

{

}

}

Hello my friend,

Your code is fine. It is not allowing you to delete the product from the product table because a related record also exists in the Order Details table. If you want to delete the product and its related records in the other table, go to the relationship in the database and choose the "Cascade Delete Related Records" option.

However, I would not do it this way. If I buy something from your website I will have a record in the Order Details table that links back to a product. If you delete this product and it cascades to the Order Details table, the record of my order is also lost. Instead of deleting products, create a field called "Active" in the product table that will contain either 1 or 0. Then configure your website to only show products with Active=1. A benefit of this approach is that you can always set something back to Active if you make a mistake.

Kind regards

Scotty

|||

Looks like product ID is used as foreign Key in some orders and you can not delete product as long as this reference exists, to delete record from product table you have to delete all orders in which it is included or remove connection between Order details and products table.

But I think that in case like this you should display message to user : You can not delete this product because it was used in some orders!!!

Errors using DATEDIFF and DATEADD formulas

I am trying to use the DATEDIFF function in a formula, but it isn't
working. Here is the formula that I am entering into the "Define
Formula" dialog box:
DATEDIFF("day", TimeLogged, TimeClosed)
When I click ok, it gives me the message, "The arguments to the
following function are not valid: DATEDIFF". With that in mind, I
looked in the Help file for the report builder and under the "DATEDIFF
statement" documentation, it had the following example:
DATEDIFF("month", #1/1/2009#, #3/31/2009#)
This one yielded the same results. I also tried the DATEADD formula
and got the same type of error message. Other formulas, such as YEAR,
WEEK, etc. work fine.
Does anyone have any suggestions? I get the same results using both
the Report Builder and Visual Studio 2005.Hi,
the syntax is like this.
DATEDIFF("d", TimeLogged, TimeClosed)
This will work.
Amarnath.
"photogulliver" wrote:
> I am trying to use the DATEDIFF function in a formula, but it isn't
> working. Here is the formula that I am entering into the "Define
> Formula" dialog box:
> DATEDIFF("day", TimeLogged, TimeClosed)
> When I click ok, it gives me the message, "The arguments to the
> following function are not valid: DATEDIFF". With that in mind, I
> looked in the Help file for the report builder and under the "DATEDIFF
> statement" documentation, it had the following example:
> DATEDIFF("month", #1/1/2009#, #3/31/2009#)
> This one yielded the same results. I also tried the DATEADD formula
> and got the same type of error message. Other formulas, such as YEAR,
> WEEK, etc. work fine.
> Does anyone have any suggestions? I get the same results using both
> the Report Builder and Visual Studio 2005.
>

Sunday, March 11, 2012

errors in storedprocedures

Is there a function that will indicate if there is an error in your stored procedure? Like for example, I want to create a stored procedure and if there is an error I want it to print out an error. How can I do this?It' called the query analyzer and there are 2 options the parse button and the execute button.|||Thanks, I am aware of the query panel. Are you suggestion that there isn't anything else? I know you can use the SQL server agent or DTS to do some error handling on your job but I wanted to know if there was a function you could add similar to the onerror function in visual basic.|||Check @.@.error in BOL.|||Sure

http://weblogs.sqlteam.com/brettk/archive/2004/05/25/1378.aspx

Sunday, February 26, 2012

Error:Procedure or function CatalogJSummary has too many arguments specified

hello, all

I am using a Stored procedure and calling this in my code in C#.

Here is my procedure:

CatalogJSummary

(

@.SeriesId INT

)

AS

SELECT Games.GameName, Games.Id AS GameId

FROM GameCodes INNER JOIN

Games ON GameCodes.GameId = Games.Id INNER JOIN

MobileSeries ON GameCodes.SeriesId = MobileSeries.Id INNER JOIN

Mobiles ON MobileSeries.Id = Mobiles.SeriesId

GROUP BY Games.GameName, Games.Id, MobileSeries.Id

HAVING (MobileSeries.Id = @.SeriesId)

RETURN

and here is the calling C# code::

public DataSet CatalogJSummary(int sid)

{

SqlConnection cnUnique=new SqlConnection();

cnUnique.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["jconnection"];

//con.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["jconnection"];

com.CommandText="CatalogJSummary";

com.CommandType=CommandType.StoredProcedure;

com.Parameters.Add("@.SeriesId",SqlDbType.Int);

com.Parameters["@.SeriesId"].Value=sid;

com.Connection=cnUnique;

// con.Open();

cnUnique.Open();

adp=new SqlDataAdapter();

adp.SelectCommand=com;

DataSet ds1=new DataSet();

adp.Fill(ds1);

cnUnique.Close();

return ds1;

}

but when this code execute,with the parameter passed to stored procedure using this code at runtime, i get the following error::

Procedure or function CatalogJSummary has too many arguments specified.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Procedure or function CatalogJSummary has too many arguments specified.

Source Error:

Line 243:adp.SelectCommand=com; Line 244:DataSet ds1=new DataSet(); Line 245:adp.Fill(ds1); Line 246:cnUnique.Close(); Line 247:return ds1;


Source File: c:\inetpub\wwwroot\mobmasti.catalog\classes\truetonescls.cs Line: 245

Anyone please help me to resolve this error

thanks in advance

There are many options you can try:

1. From SQL Server books online:
If the first three characters of the procedure name are sp_, SQL Server
searches the master database for the procedure. If no qualified procedure name is provided, SQL Server searches for the procedure as if the owner name is dbo. To resolve the stored procedure name as a user-defined stored procedure with the same name as a system stored procedure, provide the fully qualified procedure name.

2. Make sure the field names between your calls and the procedure are the same

3. Make sure you attached the database with the user it was intended to be used with. Otherwise you may not have sufficient rights and this message will show up.

Sunday, February 19, 2012

Error: The MDX function ROOT failed because...

I'm having problem calculating sales for all customer minus certain occupations.

This Adventureworks query is something like what Excel 2003 produces.

Code Snippet

WITH MEMBER [Customer].[Occupation].[Exclude Occupations] AS

'AGGREGATE({

[Customer].[Occupation].&[Clerical],

[Customer].[Occupation].&[Management]

})'

MEMBER Measures.[Other Occupation Sales] as (ROOT(Customer), [Measures].[Internet Sales Amount]) - [Measures].[Internet Sales Amount]

SELECT

{Measures.[Other Occupation Sales]} ON COLUMNS,

[Product].[Category].[Category].MEMBERS ON ROWS

FROM [Adventure Works]

WHERE

([Customer].[Occupation].[Exclude Occupations])

Error: The MDX function ROOT failed because the coordinate for the 'Occupation' attribute contains a set.

If you delete this line, the query works!

[Customer].[Occupation].&[Clerical],

Does anyone know what this means? Is there another way to get Excel to formulate an equivalent query?

The problem is because there is a set in the current coordinate, and Root has problem dealing with it (although it shouldn't!). The best solution is to replace Root(Customer) with [All Customers] - which will have exactly same functionality, better performance, and won't generate the error that started this thread.|||

Yes, I replaced Root(Customer) with [Customer].[Occupation].[All Customers] and it works. The problem is that I wanted to create a general purpose calculated measure that would override every attribute in the customer dimension with "All". I thought Root did precisely that. If I can't use Root() then I will need to remember to update this calculation every time an attribute is added or removed from the customer dimension.

Thanks for the input.

|||

> The problem is that I wanted to create a general purpose calculated measure that would override every attribute in the customer dimension with "All"

[All Customers] does exactly that ! There is no need to update your calculation every time new attribute is added, [All Customers] (in any hierarchy of customers dimension) will automatically move coordinate to All member in every attribute.

|||

Try my AdventureWorks repro with [All Customers] instead of Root(). It doesn't work. It doesn't replace the aggregate set of occupations with the All tuple. I end up with zeros in the results because [All Customers] is doing absolutely nothing for me.

The only way I got it to work was with [Customer].[Occupation].[All Customers]. This member does, in fact, replace my aggregate set of occupations.

Put that in your pipe and smoke it!

I'm curious how [All Customers] is defined to work (vs. Root()).

|||Of course - you are right. I don't know what I was thinking. What I said is only true when dimension has single hierarchy which includes all attributes, which is obviously not the case for most of the dimensions, especially Customer.|||Thanks for the help. At least I have a work-around for now. It would be nice if Root(dimension) did the same thing as change coordinates to [All Customers] on every attribute. Maybe in the next version...