I'm attempting to use a SqlDataSource in lieu of existing code and I'm not having much success. Take a look at what I'm trying to do, perhaps there's a better way.
asp:Content
ID="Content1"
ContentPlaceHolderID="ContentPlaceHolder1"
runat="Server">
<div
id="Body">
<div
class="fullwidth">
<h2>
Add Blog Post</h2>
<p>
<asp:Label
ID="BlogDate"
runat="server"
Text="Blog Time Stamp:"></asp:Label>
<asp:Label
ID="TodayDate"
runat="server">
</asp:Label><strong><br
/><br
/>Blog Title<br
/>
<asp:TextBox
ID="txtTitle"
runat="server"
Width="640px">
</asp:TextBox></strong>
</p>
<p><strong>Blog
Text<br
/>
<asp:TextBox
ID="txtEntry"
runat="server"
Height="300px"
Width="640px"></asp:TextBox></strong>
</p>
<p>
<asp:Button
ID="add"
runat="server"
Text="Create New Blog"
/>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MHSVB1ConnectionString %>"
OnSelecting="SqlDataSource1_Selecting"
SelectCommand="SELECT * FROM aspnet_membership(userID) WHERE (userID = @memid)"
InsertCommand="INSERT INTO BlogPosts(Title,
PostText, postdate, memid) VALUES (@Title,@PostText,@postdate,@memid)"
ProviderName="System.Data.SqlClient">
<InsertParameters>
<asp:ControlParameter
ControlID="txtTitle"
Name="Title"
PropertyName="Text"
/>
<asp:ControlParameter
ControlID="txtEntry"
Name="PostText"
PropertyName="Text"
/>
<asp:ControlParameter
ControlID="TodayDate"
Name="postdate"
PropertyName="Text"
/>
<asp:Parameter
Name="memid"
Type="Object"
/>
</InsertParameters>
</asp:SqlDataSource>
</p>
</div>
</div>
</asp:Content>
Code Behind:
Partial
Class Blogs_AddPost
Inherits System.Web.UI.Page
Protected
Sub Page_Load(ByVal sender
As
Object,
ByVal e
As System.EventArgs)
Handles
Me.Load
If User.IsInRole("Blogger") =
False
Then
Response.Redirect("~/blogs/default.aspx")
End
If
Session("FCKeditor:UserFilesPath") = Request.ApplicationPath.ToString &
"Files/"
TodayDate.Text = Date.Now
End
Sub
Protected
Sub SqlDataSource1_Selecting(ByVal sender
As
Object,
ByVal e
As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)
Handles SqlDataSource1.Selecting
Dim currentUser
As MembershipUser = Membership.GetUser()
Dim currentUserId
As Guid =
CType(currentUser.ProviderUserKey, Guid)
e.Command.Parameters("@memid").Value = currentUserId
Response.Redirect("~/blogs/default.aspx")
End
Sub
Protected
Sub Add_Click(ByVal sender
As
Object,
ByVal e
As System.EventArgs)
Handles add.Click
SqlDataSource1.Insert()
End
Sub
End Class
As the current Source Code and Code Behind that I have posted here. When executed I receive the following error: 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: Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query.
Source Error:
Line 21:
Line 22: Protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles add.Click
Line 23: SqlDataSource1.Insert()
Line 24: End Sub
Line 25: End Class
Source File: ~\blogs\AddPost.aspx.vb Line: 23
Stack Trace:
[SqlException (0x80131904): Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1948826
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4844747
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +204
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +175
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +386
System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary values) +227
System.Web.UI.WebControls.SqlDataSource.Insert() +16
Blogs_AddPost.Add_Click(Object sender, EventArgs e) in ~\blogs\AddPost.aspx.vb:23
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
I can add a DefaultValue to the InsertParameter [memid] and it will work all day long. However that's not the solution here.
<asp:Parameter
Name="memid"
DefaultValue="7bb2c179-65d9-45b2-b54a-b93371db50f0"
Type="String"
/>
I'm to the point of begging, would someone please offer some advise on how to correct this issue.
hspreps
Member
6 Points
9 Posts
Re: Problems and Solutions for running CSK 2.0 in a Medium Trust Enviroment
Feb 19, 2009 08:42 PM|LINK
Issue: Creating New Blogs
I'm attempting to use a SqlDataSource in lieu of existing code and I'm not having much success. Take a look at what I'm trying to do, perhaps there's a better way.
File Path: ~/Blogs/addPost.aspx
Source Code:<%
@ Page Title="" Language="VB" MasterPageFile="~/Default.master" AutoEventWireup="false"CodeFile="AddPost.aspx.vb" Inherits="Blogs_AddPost" %>
<
asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"><div id="Body">
<div class="fullwidth">
<h2>
Add Blog Post</h2>
<p>
<asp:Label ID="BlogDate" runat="server" Text="Blog Time Stamp:"></asp:Label>
<asp:Label ID="TodayDate" runat="server">
</asp:Label><strong><br /><br />Blog Title<br />
<asp:TextBox ID="txtTitle" runat="server" Width="640px"> </asp:TextBox></strong>
</p>
<p><strong>Blog Text<br />
<asp:TextBox ID="txtEntry" runat="server" Height="300px" Width="640px"></asp:TextBox></strong>
</p>
<p>
<asp:Button ID="add" runat="server" Text="Create New Blog" /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MHSVB1ConnectionString %>"
OnSelecting="SqlDataSource1_Selecting"
SelectCommand="SELECT * FROM aspnet_membership(userID) WHERE (userID = @memid)"
InsertCommand="INSERT INTO BlogPosts(Title, PostText, postdate, memid) VALUES (@Title,@PostText,@postdate,@memid)"
ProviderName="System.Data.SqlClient"> <InsertParameters>
<asp:ControlParameter ControlID="txtTitle" Name="Title" PropertyName="Text" />
<asp:ControlParameter ControlID="txtEntry" Name="PostText" PropertyName="Text" />
<asp:ControlParameter ControlID="TodayDate" Name="postdate" PropertyName="Text" />
<asp:Parameter Name="memid" Type="Object" />
</InsertParameters>
</asp:SqlDataSource> </p>
</div>
</div>
</asp:Content> Code Behind:
Partial Class Blogs_AddPost
Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If User.IsInRole("Blogger") = False Then
Response.Redirect("~/blogs/default.aspx") End If Session("FCKeditor:UserFilesPath") = Request.ApplicationPath.ToString & "Files/"
TodayDate.Text = Date.Now End Sub Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Selecting Dim currentUser As MembershipUser = Membership.GetUser()
Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)
e.Command.Parameters("@memid").Value = currentUserId Response.Redirect("~/blogs/default.aspx") End Sub Protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles add.Click
SqlDataSource1.Insert() End Sub End Class
As the current Source Code and Code Behind that I have posted here. When executed I receive the following error:
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: Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query.
Source Error:
Source File: ~\blogs\AddPost.aspx.vb Line: 23
Stack Trace:
Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
I can add a DefaultValue to the InsertParameter [memid] and it will work all day long. However that's not the solution here.
<asp:Parameter Name="memid" DefaultValue="7bb2c179-65d9-45b2-b54a-b93371db50f0" Type="String" /> I'm to the point of begging, would someone please offer some advise on how to correct this issue.