all groups > asp.net webcontrols > january 2006 >
You're in the

asp.net webcontrols

group:

detailsview, inserting new record and request.querystring


detailsview, inserting new record and request.querystring TdarTdar
1/31/2006 1:14:34 PM
asp.net webcontrols:
Hello,
I have a value from another page called shipkey, I would like to use this
value
to populate the field shipkey in the detailsview for inserting a new record.
how would that be done correctly?

code:

<%@ Page Language="VB" MasterPageFile="~/Site.master"
AutoEventWireup="false" CodeFile="AddPackage.aspx.vb"
Inherits="Shipping_PreparePackage_AddPackage" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<br />
<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False" DataKeyNames="CartonKey,ShipKey"
DataSourceID="SqlDataSource1" DefaultMode="Insert" Height="50px"
Width="125px">
<Fields>
<asp:BoundField DataField="CartonKey" HeaderText="CartonKey"
SortExpression="CartonKey" InsertVisible="False" />
<asp:BoundField DataField="ShipKey" HeaderText="ShipKey"
SortExpression="ShipKey" InsertVisible="False" NullDisplayText='' />
<asp:BoundField DataField="PackageID" HeaderText="PackageID"
SortExpression="PackageID" />
<asp:BoundField DataField="Length" HeaderText="Length"
SortExpression="Length" />
<asp:BoundField DataField="width" HeaderText="width"
SortExpression="width" />
<asp:CheckBoxField DataField="Breakable" HeaderText="Breakable"
SortExpression="Breakable" />
<asp:BoundField DataField="TrackingNumber"
HeaderText="TrackingNumber" SortExpression="TrackingNumber" />
<asp:BoundField DataField="Weight" HeaderText="Weight"
SortExpression="Weight" />
<asp:BoundField DataField="OverSize12" HeaderText="OverSize12"
SortExpression="OverSize12" />
<asp:BoundField DataField="height" HeaderText="height"
SortExpression="height" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$
ConnectionStrings:SilverQueen_Main_SystemConnectionString1 %>"
DeleteCommand="DELETE FROM [ShipCartonRecord] WHERE [CartonKey] =
@CartonKey AND [ShipKey] = @ShipKey"
InsertCommand="INSERT INTO [ShipCartonRecord] ([ShipKey],
[PackageID], [Length], [width], [Breakable], [TrackingNumber], [Weight],
[OverSize12], [height]) VALUES (@ShipKey, @PackageID, @Length, @width,
@Breakable, @TrackingNumber, @Weight, @OverSize12, @height)"
SelectCommand="SELECT [CartonKey], [ShipKey], [PackageID], [Length],
[width], [Breakable], [TrackingNumber], [Weight], [OverSize12], [height] FROM
[ShipCartonRecord] WHERE ([ShipKey] = @ShipKey)"
UpdateCommand="UPDATE [ShipCartonRecord] SET [PackageID] =
@PackageID, [Length] = @Length, [width] = @width, [Breakable] = @Breakable,
[TrackingNumber] = @TrackingNumber, [Weight] = @Weight, [OverSize12] =
@OverSize12, [height] = @height WHERE [CartonKey] = @CartonKey AND [ShipKey]
= @ShipKey">
<DeleteParameters>
<asp:Parameter Name="CartonKey" Type="Object" />
<asp:Parameter Name="ShipKey" Type="Object" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="PackageID" Type="String" />
<asp:Parameter Name="Length" Type="Int16" />
<asp:Parameter Name="width" Type="Int16" />
<asp:Parameter Name="Breakable" Type="Boolean" />
<asp:Parameter Name="TrackingNumber" Type="String" />
<asp:Parameter Name="Weight" Type="String" />
<asp:Parameter Name="OverSize12" Type="String" />
<asp:Parameter Name="height" Type="Int16" />
<asp:Parameter Name="CartonKey" Type="Object" />
<asp:Parameter Name="ShipKey" Type="Object" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="ShipKey"
QueryStringField="ShipKey" Type="Object" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="CartonKey" Type="Object" />
<asp:Parameter Name="ShipKey" Type="Object" />
<asp:Parameter Name="PackageID" Type="String" />
<asp:Parameter Name="Length" Type="Int16" />
<asp:Parameter Name="width" Type="Int16" />
<asp:Parameter Name="Breakable" Type="Boolean" />
<asp:Parameter Name="TrackingNumber" Type="String" />
<asp:Parameter Name="Weight" Type="String" />
<asp:Parameter Name="OverSize12" Type="String" />
<asp:Parameter Name="height" Type="Int16" />
</InsertParameters>
</asp:SqlDataSource>
</asp:Content>

RE: detailsview, inserting new record and request.querystring stcheng NO[at]SPAM online.microsoft.com
2/1/2006 12:00:00 AM
Hi Tdar,

As for the question about populate field of input control in DetailsView
(insertMode), I think we need to manually access the control collection and
find the control reference and assign the certain value(initial value). Is
the value passed from other page in the url querystring?

Anyway, since BoundField's insert textbox control is autogenerated, it'll
be hard for us to locate it through ID or index. IMO, we can convert that
field(column) into a template column so that we can use FindControl to
locate the control through the control ID. e.g:

the following page template use a DetailsView with one template
field(converted from bound field) for one column that need to set initial
value from querystring:

============================================
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID] =
@CategoryID" InsertCommand="INSERT INTO [Categories] ([CategoryName],
[Description]) VALUES (@CategoryName, @Description)"
SelectCommand="SELECT [CategoryID], [CategoryName],
[Description] FROM [Categories]"
UpdateCommand="UPDATE [Categories] SET [CategoryName] =
@CategoryName, [Description] = @Description WHERE [CategoryID] =
@CategoryID">
<DeleteParameters>
<asp:Parameter Name="CategoryID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="CategoryID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="Description" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server"
AllowPaging="True" AutoGenerateRows="False"
DataKeyNames="CategoryID" DataSourceID="SqlDataSource1"
DefaultMode="Insert"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="CategoryID"
HeaderText="CategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:TemplateField HeaderText="CategoryName"
SortExpression="CategoryName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("CategoryName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("CategoryName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("CategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description"
HeaderText="Description" SortExpression="Description" />
<asp:CommandField ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
================================

and below is the code in page_load where we get the value from url
querystring and set it to the input field in detailsview's insert template.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string initVal = Request.QueryString["category"];
if (!string.IsNullOrEmpty(initVal))
{

((TextBox)DetailsView1.FindControl("TextBox1")).Text =
initVal ;
}
}
}


Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
RE: detailsview, inserting new record and request.querystring TdarTdar
2/1/2006 8:40:34 AM
That worked thanks but I am getting that stupid "Disallowed implicit
conversion from data type sql_variant to data type uniqueidentifier, table
'SilverQueen_Main_System.dbo.ShipCartonRecord', column 'ShipKey'. Use the
CONVERT function to run this query. "

again ugh I checked the readonly field properties and the description that
said guid in that other problem we had before. I also created a new page and
tested the passed querystring a GUID to a test table(Ui data field in that
table) and it worked fine with the code you provided here.
So i know the data that I am putting into that field is correct and the
changes you described in this tread work, but then i get this error, ugh. Oh
cartonkey is the
Key field in the table.


addpackage.aspx
==========


<%@ Page Language="VB" MasterPageFile="~/Site.master"
AutoEventWireup="false" CodeFile="AddPackage.aspx.vb"
Inherits="Shipping_PreparePackage_AddPackage" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False" DataKeyNames="CartonKey,ShipKey"
DataSourceID="SqlDataSource1" DefaultMode="Insert" Height="50px"
Width="125px">
<Fields>
<asp:BoundField DataField="CartonKey" HeaderText="CartonKey"
SortExpression="CartonKey" InsertVisible="False" />
<asp:TemplateField HeaderText="ShipKey" SortExpression="ShipKey">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Eval("ShipKey") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="ShipKeyTxt" runat="server" Text='<%#
Bind("ShipKey") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("ShipKey") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PackageID" HeaderText="PackageID"
SortExpression="PackageID" />
<asp:BoundField DataField="Length" HeaderText="Length"
SortExpression="Length" />
<asp:BoundField DataField="width" HeaderText="width"
SortExpression="width" />
<asp:BoundField DataField="OverSize12" HeaderText="OverSize12"
SortExpression="OverSize12" />
<asp:BoundField DataField="height" HeaderText="height"
SortExpression="height" />
<asp:BoundField DataField="TrackingNumber"
HeaderText="TrackingNumber" SortExpression="TrackingNumber" />
<asp:BoundField DataField="Weight" HeaderText="Weight"
SortExpression="Weight" />
<asp:CheckBoxField DataField="Breakable" HeaderText="Breakable"
SortExpression="Breakable" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$
ConnectionStrings:SilverQueen_Main_SystemConnectionString1 %>"
DeleteCommand="DELETE FROM [ShipCartonRecord] WHERE [CartonKey] =
@CartonKey AND [ShipKey] = @ShipKey"
InsertCommand="INSERT INTO [ShipCartonRecord] ([ShipKey],
[PackageID], [Length], [width], [OverSize12], [height], [TrackingNumber],
[Weight], [Breakable]) VALUES (@ShipKey, @PackageID, @Length, @width,
@OverSize12, @height, @TrackingNumber, @Weight, @Breakable)"
SelectCommand="SELECT [CartonKey], [ShipKey], [PackageID], [Length],
[width], [OverSize12], [height], [TrackingNumber], [Weight], [Breakable] FROM
[ShipCartonRecord] WHERE ([ShipKey] = @ShipKey)"
UpdateCommand="UPDATE [ShipCartonRecord] SET [PackageID] =
@PackageID, [Length] = @Length, [width] = @width, [OverSize12] = @OverSize12,
[height] = @height, [TrackingNumber] = @TrackingNumber, [Weight] = @Weight,
[Breakable] = @Breakable WHERE [CartonKey] = @CartonKey AND [ShipKey] =
@ShipKey">
<DeleteParameters>
<asp:Parameter Name="CartonKey" Type="Object" />
<asp:Parameter Name="ShipKey" Type="Object" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="PackageID" Type="String" />
<asp:Parameter Name="Length" Type="Int16" />
<asp:Parameter Name="width" Type="Int16" />
<asp:Parameter Name="OverSize12" Type="String" />
<asp:Parameter Name="height" Type="Int16" />
<asp:Parameter Name="TrackingNumber" Type="String" />
<asp:Parameter Name="Weight" Type="String" />
<asp:Parameter Name="Breakable" Type="Boolean" />
<asp:Parameter Name="CartonKey" Type="Object" />
<asp:Parameter Name="ShipKey" Type="Object" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter DefaultValue="-1" Name="ShipKey"
QueryStringField="ShipKey"
Type="Object" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="ShipKey" Type="Object" />
<asp:Parameter Name="PackageID" Type="String" />
<asp:Parameter Name="Length" Type="Int16" />
<asp:Parameter Name="width" Type="Int16" />
<asp:Parameter Name="OverSize12" Type="String" />
<asp:Parameter Name="height" Type="Int16" />
<asp:Parameter Name="TrackingNumber" Type="String" />
<asp:Parameter Name="Weight" Type="String" />
<asp:Parameter Name="Breakable" Type="Boolean" />
</InsertParameters>
</asp:SqlDataSource>

</asp:Content>




RE: detailsview, inserting new record and request.querystring stcheng NO[at]SPAM online.microsoft.com
2/6/2006 12:00:00 AM
Hi Tdar,

Thanks for your response.
So your current problem turn to be type converting for guid type? To make
the problem simlified and specific, can you try creating a simple datatable
,with just 2 or 3 columns(and the guild column as the PK) and test it in
the page to reproduce the problem? That's can help us concencrate on the
concrete problem.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)





RE: detailsview, inserting new record and request.querystring TdarTdar
2/6/2006 2:31:24 PM
Ok, but this is a second guid in the table not the guid that is pk..
PackageKey GUID <-- this is pk field
ShipKey GUID <-- this is second field and give me the problem.

If you want I can /well try to reporduce this in a small table/form.

But....

.. Stepping back maybe I should not be using guid.. If I intend to
replicate the database I though i needed guid, so i use using that instead of
a stepping int.. as a pk. I guess I dont know really where guid should be
used verses a stepping intergers.

If i get rid of the guid I guess I eliminate this problem I keep running
into, but I thought I saw something where you need to use guid if you are
going to replicate
your sql data. I have not had a chance to watch "A PRIMER TO PROPER SQL
SERVER DEVELOPMENT "



[quoted text, click to view]
RE: detailsview, inserting new record and request.querystring stcheng NO[at]SPAM online.microsoft.com
2/7/2006 12:00:00 AM
Thanks for your followup Tdar,

Yes, I also think Int/bigint is sufficient for normal table's primarykey.
Guid is not frequently used, and sometimes if you want to keep the record
in one table also unique in more wide scope(e.g the table may be merged
with some other table on different server), they'll use Guid as PK to avoid
confliction. If you can get your page and code logic work with int, I
think just use int , that'll make things much easier...

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
RE: detailsview, inserting new record and request.querystring TdarTdar
2/9/2006 8:04:29 AM
Hello,
I after much needed sleep, think I found where the problem is I have this
code
==================

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Dim Shpky As String, Shiptxt As TextBox
Shpky = Request.QueryString("ShipKey")
Shiptxt = CType(DetailsView1.FindControl("ShipKeyTxt"), TextBox)
Shiptxt.Text = Shpky.ToString

End If
End Sub
==================================
the error being "conversion from data type sql_variant to data type
uniqueidentifier"

I am getting the GUID from a previous page by "Request.QueryString("ShipKey")"
then passing it into the DetailsView1.FindControl("ShipKeyTxt"), TextBox
I also tried Shiptxt.Text = Shpky to get the same error. So what is
happening is that at some point the GUID is getting converted to a
sql_variant how cani make sure this is passed as a GUID and not changed. I
Thought maybe that because I noticed else where that the "{" gets stripped
off the GUID when using it in certian context I dont re-call at the moment.
Anyways I also tried Shiptxt.Text = "{" & Shpky.ToString & "}" to try to
get it to a value it expects.

So how do i keep the GUID from getting converted to sql_varient?

Tdar

p.s. thanks for your comments, they are helpfull but since I think this is
the problem I would like to try to resolve this, since I will requre GUID in
other areas of this system.



[quoted text, click to view]
RE: detailsview, inserting new record and request.querystring stcheng NO[at]SPAM online.microsoft.com
2/13/2006 12:00:00 AM
Hi Tdar,

I've just performed some further test. I think the casting exception is
likely due to the Type="Object" attribute setting of the Guid Column's
Parameter in the SqlDataSource. You can try removing it to see whether it
works. e.g:

================
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:FileTestDB %>"
InsertCommand="INSERT INTO [TestTable] ([name], [uuid]) VALUES
(@name, CONVERT(uniqueidentifier, @uuid))"
[quoted text, click to view]
.................................
<InsertParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="uuid" />
</InsertParameters>
</asp:SqlDataSource>
=======================

it works in my local test. Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
RE: detailsview, inserting new record and request.querystring stcheng NO[at]SPAM online.microsoft.com
2/15/2006 12:00:00 AM
Hi Tdar,

Does my further suggestion helps? If you're still meeting any problem,
please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
RE: detailsview, inserting new record and request.querystring nate
7/24/2006 2:00:02 PM
It appeared that Tdar was using the querystring to pass the variables.
I am having a similar problem but in my case I am using an SqlDataSource.
I have 3 GUIDs and only one of them is described as a uniqueidentifier type
in the code that is being sent to the SQLserver.
If I profile the Update string it ends up looking like the following:
Note that I added the Casts in there because I was getting the conversion
error.

exec sp_executesql N'UPDATE Record SET Approved = @Approved, ReviewedBy =
CAST(@ReviewedBy AS uniqueidentifier), DataOriginationDate =
@DataOriginationDate WHERE (CaseGUID = CAST(@original_CaseGUID AS
uniqueidentifier)) AND (Approved = @original_Approved) AND (ReviewedBy =
CAST(@original_ReviewedBy AS uniqueidentifier)) AND (Creator =
CAST(@original_Creator AS uniqueidentifier)) AND (CreationDate =
@original_CreationDate) AND (DataOriginationDate =
@original_DataOriginationDate)',
N'@Approved bit,@ReviewedBy nvarchar(36),@DataOriginationDate
nvarchar(9),@original_CaseGUID uniqueidentifier,@original_Approved
bit,@original_ReviewedBy nvarchar(36),@original_Creator
nvarchar(36),@original_CreationDate datetime,@original_DataOriginationDate
nvarchar(4000),@Creator nvarchar(36)', @Approved = 1, @ReviewedBy =
N'dde05039-9011-4a9b-a3b2-c19a21f5bd18', @DataOriginationDate = N'9/20/2006',
@original_CaseGUID = '8BC71CED-F365-4AB1-AFE1-F2316578B2CC',
@original_Approved = 1, @original_ReviewedBy =
N'dde05039-9011-4a9b-a3b2-c19a21f5bd18', @original_Creator =
N'90d9dfe7-b2ed-4146-ad0b-62be60ff627e', @original_CreationDate = NULL,
@original_DataOriginationDate = NULL, @Creator =
N'90d9dfe7-b2ed-4146-ad0b-62be60ff627e'


The second parameter string of the sp_executesql call shows that most of the
GUIDs are declared as nvarchar(36), whereas; original_caseGUID is declared
correctly as uniqueidentifier.
I have declared the GUID parameters in the aspx page without the TYPE= as
many folks seem to say works. And that does seem to work for various
situations but doesn't seem to work for the SQLdatasource.

Is there a way to get at what SQLdatasource is using to determine the Type
when generating the exec string?

HERE is my aspx params etc.
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$
ConnectionStrings:ISOLSConnectionString %>" DeleteCommand="DELETE FROM
[Record] WHERE [CaseGUID] = @original_CaseGUID AND [Approved] =
@original_Approved AND [ReviewedBy] = @original_ReviewedBy AND [Creator] =
@original_Creator AND [CreationDate] = @original_CreationDate AND
[DataOriginationDate] = @original_DataOriginationDate"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT Approved, ReviewedBy, Creator, CreationDate,
DataOriginationDate, CaseGUID FROM Record WHERE (CaseGUID = @CaseGUID)"
UpdateCommand="UPDATE Record SET Approved = @Approved,
ReviewedBy = CAST(@ReviewedBy AS uniqueidentifier), DataOriginationDate =
@DataOriginationDate WHERE (CaseGUID = CAST(@original_CaseGUID AS
uniqueidentifier)) AND (Approved = @original_Approved) AND (ReviewedBy =
CAST(@original_ReviewedBy AS uniqueidentifier)) AND (Creator =
CAST(@original_Creator AS uniqueidentifier)) AND (CreationDate =
@original_CreationDate) AND (DataOriginationDate =
@original_DataOriginationDate)">
<DeleteParameters>
<asp:Parameter Name="original_CaseGUID" />
<asp:Parameter Name="original_Approved"
Type="Boolean" />
<asp:Parameter Name="original_ReviewedBy" />
<asp:Parameter Name="original_Creator" />
<asp:Parameter Name="original_CreationDate"
Type="DateTime" />
<asp:Parameter Name="original_DataOriginationDate" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Approved" Type="Boolean" />
<asp:Parameter Name="ReviewedBy" />
<asp:Parameter Name="DataOriginationDate" />
<asp:Parameter Name="original_CaseGUID" />
<asp:Parameter Name="original_Approved"
Type="Boolean" />
<asp:Parameter Name="original_ReviewedBy" />
<asp:Parameter Name="original_Creator" />
<asp:Parameter Name="original_CreationDate"
Type="DateTime" />
<asp:Parameter Name="original_DataOriginationDate" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1"
DefaultValue="{771DD329-1A00-4078-AD23-048C7D2E575F}"
Name="CaseGUID" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>





[quoted text, click to view]
AddThis Social Bookmark Button