Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Wednesday, March 28, 2012

Parameter name Questions in SqlDataSource??

I have a table with with some column name includes a space. for example [Product ID] [Product Name] Instead of Product_ID, Product_Name. when I try to create a gridview and enable delete, insert. It just won't work.

I've been trying for several hours without success. When I click on delete. the page postback without any error, but the record doesn't get deleted or updated.

<asp:SqlDataSource id="sourceProducts" runat="server"
SelectCommand="SELECT [Product ID], [Product Name] FROM Products" ConnectionString="<%$ ConnectionStrings:mydb %>"
DeleteCommand="Delete from Products where [Product ID]=@.ProductID
UpdateCommand="UPDATE Products SET [Product Name]=@.ProductName WHERE [Product ID]=@.ProductID"
<UpdateParameters>
<asp:Parameter Name="ProductName" />
<asp:Parameter Name="ProductID" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32"/>
</DeleteParameters>
</asp:SqlDataSource
<asp:GridView ID="GridView2" runat="server" DataSourceID="sourceProducts"
AutoGenerateColumns="False" DataKeyNames="Product ID" >
<Columns>
<asp:BoundField DataField="Product ID" HeaderText="ID" ReadOnly="True" />
<asp:BoundField DataField="Product Name" HeaderText="Product Name"/>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="True">
</asp:GridView>

Another testing I did was to use another table with no space in the Column name, Product_ID, Product_Name. and I can't name my parameter as PID, PNAME. I have to name it as @.Product_ID, @.Product_Name in order for Delete, update to work. My understanding is if I declare the parameter explicitly(<asp:Parameter Name="PID" />, I can use any name I want. Did I must missed something?

I'm new to ASP.NET, could someone help me?

Thanks.

First off, you're setting yourself up for trouble by using field names with spaces. In this case, the problem is with the discrepancy between your Parameter name "ProductID" and your DataKeyNames setting of "Product ID". When you are attempting to update or delete a record, your primary key Parameter is not being set as it's name differs from that of your DataKeyNames setting. The two need to be the same in order for this to work, but you also can't use a Parameter with a space in it at least in good practice. Therefore, switch back to using unspaced field names and make sure the primary key Parameter matches that of the DataKeyNames setting.

|||

Thanks for explaining the cause of the problem, it really helps. The above code is a simple demo, and I don't have control over the existing Database.

Is there a way to work arround it?

ddpp

|||

Your workaround will be to set these Parameters in code. By using the SqlDataSource.Updating and Deleting events, you can manually extract the DataKey from the GridView and set the command Parameter to its value. With this method, the parameters can have different names.

|||

Ed, thanks for the tip. It guides me to the solution

<script runat="server"> void SqlDataSource1_Updating(Object sender, System.Web.UI.WebControls.SqlDataSourceCommandEventArgs e) { e.Command.Parameters["@.id"].Value = e.Command.Parameters["@.ContactID"].Value; e.Command.Parameters["@.name"].Value = e.Command.Parameters["@.ContactName"].Value; e.Command.Parameters.Remove(e.Command.Parameters["@.ContactID"]); e.Command.Parameters.Remove(e.Command.Parameters["@.ContactName"]); }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Parameter Name Mapping</title></head><body> <form id="form1" runat="server"> <div> <asp:GridView AutoGenerateColumns="False" DataKeyNames="ContactID" DataSourceID="SqlDataSource1" ID="GridView1" runat="server"> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:BoundField DataField="ContactID" HeaderText="ContactID" InsertVisible="False" ReadOnly="True" SortExpression="ContactID" /> <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" /> </Columns> </asp:GridView> <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:Contacts%>" ID="SqlDataSource1" runat="server" SelectCommand="SELECT [ContactID], [ContactName] FROM [Contacts]" UpdateCommand="UpdateContactName" UpdateCommandType="StoredProcedure" OnUpdating="SqlDataSource1_Updating"> <UpdateParameters> <asp:Parameter Name="id" Type="Int32" /> <asp:Parameter Name="name" Type="String" /> </UpdateParameters> </asp:SqlDataSource> </div> </form></body></html>

sql

Tuesday, March 20, 2012

Pair of records

Hi folks,

I am trying to write a query to get data in pairs, for example, i have data like this:
sr_no week_no
1 24-A
2 24-B
3 24-C
4 25-A
5 25-B
6 26-A
7 26-B

I want to get data in pairs i.e. data for week_no 24-A and 24-B will come togather? is it possible?

Any urgent help will be highly appreicated.

Thanksany suggestion on my problem?|||possibly the reason no one answered within such a short time (it is courtesy not to bump your own question at all, but if you feel you must, give it more than just a few hours) is that your question is not very clear

here's one solution:create table pairs
( sr_no smallint not null
, week_no varchar(9)
)

insert into pairs values (1, '24-A')
insert into pairs values (2, '24-B')
insert into pairs values (3, '24-C')
insert into pairs values (4, '25-A')
insert into pairs values (5, '25-B')
insert into pairs values (6, '26-A')
insert into pairs values (7, '26-B')

select one.week_no
, two.week_no
from pairs one
, pairs two
where one.sr_no
= two.sr_no - 1
and floor(one.sr_no/2)
< floor((one.sr_no+1)/2)

24-A 24-B
24-C 25-A
25-B 26-A|||I am controlling data with Week numbers instead. So i am trying to get data of 24 and 25 togather and so one. Sr. Number is nothing but increament field.

Thanks for reply Mr. r937|||your data is very confusing

how about showing some rows of real data, and then show what results you expect

and which is it, oracle or sql server?|||here is your solution:select week_no/2
, amount
from yourtable
group
by week_no/2|||week_no/2?

You lost me on that one Rudy!

Mr roomi, I think your problem is that your table does not follow basic rules of normalization. You are trying to store two values ("24", and "A", for instance) in a single field. This makes it difficult to sort or group by the column's components.

Ideally, break the column into two different columns. If that is not an option, then you can simulate a "week_number" column using a formula:
Select left(week_no, charindex('-', week_no)-1) as week_number

(Double-check the syntax. I've been in Oracle recently and my TSQL is a little rusty...)|||week_no/2?

You lost me on that one Rudy!piece o' cake, my good man

divide the week number by 2, and because it's an integer, the result is an integer

so 22/2 is 11, 23/2 is 11, 24/2 is 12, 25/2 is 12, and so on

this allows you to group the pairs of weeks -- 22 with 23, 24 with 25, and so on

as for the normalization, check the other thread, it has better sample data, there's nothing wrong with the design|||aw crap, i meant to cross link the guy's other post and forgot to

http://www.dbforums.com/t1022906.html

guy has two identical threads going on in separate forums|||Jeez, a cross-post. This is a forum, not a freaking scavenger hunt.

I was baffled how you were going to divide the string "24-A" by two...

Mr. Roomi, here is a rule of thumb:
"You cannot code a problem clearly and simply until you can state the problem clearly and simply."

Really. I deal with people all too frequently who start coding without being able to define the problem, and they never get very far.

Monday, March 12, 2012

Paging on SqlCeResultSet

Hi

I am using SqlCeResult and i want to give paging on that and read in help that you can do paging on ResultSet so . I need code example so that i can understand how to implement this in resultSet.

Thanks & Regards

Vishal

Hi,

I need to implement paging on sqlceresultset. The resultset contains about 60000 records .I want to implement paging and bind it to the grid. I read that it can be done using sqlceresultset, but did not any example anywhere.

Can someone please suggest how to acoomplish this ?

Thanks

|||

There’s no need to “page” ResultSet. RS is a live cursor which loads data only as needed, not all data at once. If bound to DataGrid it would only load visible rows and loads others only as you scroll doing “paging” for you. So just bind your grid to the RS and you're done with "paging".

Paging in Sqlserver 2000

Hello
If i have a select query and i need to get it result by paging(for example from row x to row y ) without using cursor .
Is there any way to do it ?
Nb : like ROW_NUMBER in SqlServer 2005 and ROWNUM oracle.There are several methods for doing this. Which is most efficient depends upon your specific circumstances.

One method involves fabricating a rownumber with a subquery. This can be done as a single SQL statement (like a view) but is not very efficient.

Another method is to use nested TOP statements. This method is reasonably fast, but when calling TOP N the N cannot be a variable. So you must either hard-code it or use dynamic sql.

One more method is to select your data into a temporary table with a defined identity value. You can use the identity values created as row numbers. This method is fairly fast, but requires multiple steps and so must be implemented as a procedure.

Paging in Sqlserver 2000

Hello
If i have a select query and i need to get it result by paging(for example from row x to row y ) without using cursor .
Is there any way to do it ?

Nb : Maybe like ROW_NUMBER in SqlServer 2005 and ROWNUM in oracle.

Just an example from my own test data, but you can use something like this.

Code Snippet

DECLARE @.x INT,

@.y INT

SET @.x = 2

SET @.y = 4

select TOP (@.y-@.x+1) * from testRange

WHERE ID NOT IN ( SELECT TOP (@.x-1) ID FROM testRange)

|||

The information here might help.

Paging Queries
www.aspfaq.com/2120

|||try this .Provide values for start & end variables.

Code Snippet

DECLARE @.START INT

,@.END INT

SELECT * FROM
(
SELECT ID
,RANK() OVER(ORDER BY ID) AS 'RNK'
FROM TABLE1
) T
WHERE T.RNK BETWEEN @.START AND @.END

Saturday, February 25, 2012

Pagebreak for long texts in a textbox

Hi
I got a report than can grow depending on what information it gets.
My problem is that When I have a large text for a textbox.
Example of the problem:
I have a few textgroups thats gonna be printed out on a pdf. On page 1
we start of with some adresses and stuff and in the middle of the page
its gonna print out a description. The problem is that this description
for this particular case takes up roughly 75% of a A4-page. this means
that on Page 1 the header 'description' prints in the middle of the
page, then it jumps to Page 2 and prints out the text for
'description'. This leaves half of Page 1 blank, which isnt really good
looking. How do I get RS to start filling page 1 with the text, and
then put in a pagebreak in the middle and move the rest of the text
that didnt fit in onto page 2?
And if it can sence so it doesnt break in the middle of a word that
would be nice too :)
Its a ordinary textbox that gets it values from a SQL server.
Hope anyone understood my problem and has a good solution
Regards
Peter Berglundforgot to mention..its RS2000 we use .. :)

Monday, February 20, 2012

Page number reseting

Are there any plans in RS 2005 to have greater control over page numbers with
regard to group breaks?
For example, if we have a multi-page report for each group with page numbers
"page x of y" in the page footer, we would like the page number reset on each
group break so that group 1 may have 3 pages (page 1 of 3, page 2 of 3, page
3 of 3) and group b may have 2 pages, (page 1 of 2, page 2 of 2), etc...
Thank you,You may want to check the following blog posting:
http://blogs.msdn.com/bwelcker/archive/2005/05/19/420046.aspx
It is not fully achieve what you are looking for - but at least it provides
you a way to reset the page numbers.
-- Robert
This posting is provided "AS IS" with no warranties, and confers no rights.
"Dave Sundell" <DaveSundell@.discussions.microsoft.com> wrote in message
news:0E32BF23-F294-4E2E-868C-FE5F20154A7A@.microsoft.com...
> Are there any plans in RS 2005 to have greater control over page numbers
> with
> regard to group breaks?
> For example, if we have a multi-page report for each group with page
> numbers
> "page x of y" in the page footer, we would like the page number reset on
> each
> group break so that group 1 may have 3 pages (page 1 of 3, page 2 of 3,
> page
> 3 of 3) and group b may have 2 pages, (page 1 of 2, page 2 of 2), etc...
> Thank you,
>|||Thanks for the reply. I was able to get it to work with custom code shared in
another posting. I would still like to get a reply from someone at Microsoft
on whether or not there are plans to have greater control over page number as
worded in my original post.
"Robert Bruckner [MSFT]" wrote:
> You may want to check the following blog posting:
> http://blogs.msdn.com/bwelcker/archive/2005/05/19/420046.aspx
> It is not fully achieve what you are looking for - but at least it provides
> you a way to reset the page numbers.
> -- Robert
> This posting is provided "AS IS" with no warranties, and confers no rights.
> "Dave Sundell" <DaveSundell@.discussions.microsoft.com> wrote in message
> news:0E32BF23-F294-4E2E-868C-FE5F20154A7A@.microsoft.com...
> > Are there any plans in RS 2005 to have greater control over page numbers
> > with
> > regard to group breaks?
> >
> > For example, if we have a multi-page report for each group with page
> > numbers
> > "page x of y" in the page footer, we would like the page number reset on
> > each
> > group break so that group 1 may have 3 pages (page 1 of 3, page 2 of 3,
> > page
> > 3 of 3) and group b may have 2 pages, (page 1 of 2, page 2 of 2), etc...
> >
> > Thank you,
> >
> >
>
>|||Having "document sections" within a report with independent page numbers
etc. is under consideration for inclusion in a future release. It won't be
available in the upcoming SQL Server 2005 release.
-- Robert
This posting is provided "AS IS" with no warranties, and confers no rights.
"Dave Sundell" <DaveSundell@.discussions.microsoft.com> wrote in message
news:6045763F-2BE2-496E-8654-0A07FCBCFBCB@.microsoft.com...
> Thanks for the reply. I was able to get it to work with custom code shared
> in
> another posting. I would still like to get a reply from someone at
> Microsoft
> on whether or not there are plans to have greater control over page number
> as
> worded in my original post.
> "Robert Bruckner [MSFT]" wrote:
>> You may want to check the following blog posting:
>> http://blogs.msdn.com/bwelcker/archive/2005/05/19/420046.aspx
>> It is not fully achieve what you are looking for - but at least it
>> provides
>> you a way to reset the page numbers.
>> -- Robert
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>> "Dave Sundell" <DaveSundell@.discussions.microsoft.com> wrote in message
>> news:0E32BF23-F294-4E2E-868C-FE5F20154A7A@.microsoft.com...
>> > Are there any plans in RS 2005 to have greater control over page
>> > numbers
>> > with
>> > regard to group breaks?
>> >
>> > For example, if we have a multi-page report for each group with page
>> > numbers
>> > "page x of y" in the page footer, we would like the page number reset
>> > on
>> > each
>> > group break so that group 1 may have 3 pages (page 1 of 3, page 2 of 3,
>> > page
>> > 3 of 3) and group b may have 2 pages, (page 1 of 2, page 2 of 2),
>> > etc...
>> >
>> > Thank you,
>> >
>> >
>>|||Thank you
"Robert Bruckner [MSFT]" wrote:
> Having "document sections" within a report with independent page numbers
> etc. is under consideration for inclusion in a future release. It won't be
> available in the upcoming SQL Server 2005 release.
>
> -- Robert
> This posting is provided "AS IS" with no warranties, and confers no rights.
> "Dave Sundell" <DaveSundell@.discussions.microsoft.com> wrote in message
> news:6045763F-2BE2-496E-8654-0A07FCBCFBCB@.microsoft.com...
> > Thanks for the reply. I was able to get it to work with custom code shared
> > in
> > another posting. I would still like to get a reply from someone at
> > Microsoft
> > on whether or not there are plans to have greater control over page number
> > as
> > worded in my original post.
> >
> > "Robert Bruckner [MSFT]" wrote:
> >
> >> You may want to check the following blog posting:
> >> http://blogs.msdn.com/bwelcker/archive/2005/05/19/420046.aspx
> >>
> >> It is not fully achieve what you are looking for - but at least it
> >> provides
> >> you a way to reset the page numbers.
> >>
> >> -- Robert
> >> This posting is provided "AS IS" with no warranties, and confers no
> >> rights.
> >>
> >> "Dave Sundell" <DaveSundell@.discussions.microsoft.com> wrote in message
> >> news:0E32BF23-F294-4E2E-868C-FE5F20154A7A@.microsoft.com...
> >> > Are there any plans in RS 2005 to have greater control over page
> >> > numbers
> >> > with
> >> > regard to group breaks?
> >> >
> >> > For example, if we have a multi-page report for each group with page
> >> > numbers
> >> > "page x of y" in the page footer, we would like the page number reset
> >> > on
> >> > each
> >> > group break so that group 1 may have 3 pages (page 1 of 3, page 2 of 3,
> >> > page
> >> > 3 of 3) and group b may have 2 pages, (page 1 of 2, page 2 of 2),
> >> > etc...
> >> >
> >> > Thank you,
> >> >
> >> >
> >>
> >>
> >>
>
>|||Dave,
I am trying to do the same thing. Can you post a reply with a link to the
thread with the code you are referring below, I can't seem to find it?
"I was able to get it to work with custom code shared in
another posting."
Thanks.
Shawn Shannon
"Dave Sundell" wrote:
> Thanks for the reply. I was able to get it to work with custom code shared in
> another posting. I would still like to get a reply from someone at Microsoft
> on whether or not there are plans to have greater control over page number as
> worded in my original post.
> "Robert Bruckner [MSFT]" wrote:
> > You may want to check the following blog posting:
> > http://blogs.msdn.com/bwelcker/archive/2005/05/19/420046.aspx
> >
> > It is not fully achieve what you are looking for - but at least it provides
> > you a way to reset the page numbers.
> >
> > -- Robert
> > This posting is provided "AS IS" with no warranties, and confers no rights.
> >
> > "Dave Sundell" <DaveSundell@.discussions.microsoft.com> wrote in message
> > news:0E32BF23-F294-4E2E-868C-FE5F20154A7A@.microsoft.com...
> > > Are there any plans in RS 2005 to have greater control over page numbers
> > > with
> > > regard to group breaks?
> > >
> > > For example, if we have a multi-page report for each group with page
> > > numbers
> > > "page x of y" in the page footer, we would like the page number reset on
> > > each
> > > group break so that group 1 may have 3 pages (page 1 of 3, page 2 of 3,
> > > page
> > > 3 of 3) and group b may have 2 pages, (page 1 of 2, page 2 of 2), etc...
> > >
> > > Thank you,
> > >
> > >
> >
> >
> >