Showing posts with label sqldatasource. Show all posts
Showing posts with label sqldatasource. Show all posts

Monday, March 26, 2012

Problem With Insert Using SQLDataSource

I have a table with 2 columns - "memberid" is a guid and "Interest" is a string. In an aspx page I want to read in all values of "Interest" for a given "memberid" and display approprite checkboxes as checked. The user can then change which boxes are checked and I want to record the new list of selected items in the table. I created a SQLDataSource (see below) but when I run it, I get this error:

========== Error ===========

Disallowed implicit conversion from data type sql_variant to data type uniqueidentifier, table 'DB_136571.dbo.gs_MemberInterests', column 'memberid'. Use the CONVERT function to run this query.

============================

The only use of this SQLDataSource is to delete all entries for a given user and then insert an entry for each checked checkbox. The code to set the session variables is as follows:

======== Code ============

Dim guidMemberidAs Guid =CType(user.ProviderUserKey, Guid)

Session("currmember") = guidMemberid

....

Session("currinterest") =CType(item.FindControl("CheckBox1"), CheckBox).Text

==========================

Any ideas of what I am doing wrong here?

========= SQLDataSource ==========

<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:GoodSamSiteDB %>"

DeleteCommand="DELETE FROM [gs_MemberInterests] WHERE [memberid] = @.memberid"

InsertCommand="INSERT INTO [gs_MemberInterests] ([memberid], [Interest]) VALUES (@.memberid, @.Interest)"

SelectCommand="SELECT memberid, Interest FROM gs_MemberInterests WHERE (memberid = @.memberid) AND (Interest = @.interest)">

<DeleteParameters>

<asp:ParameterName="memberid"Type="Object"/>

</DeleteParameters>

<SelectParameters>

<asp:SessionParameterName="memberid"SessionField="currmember"Type="Object"/>

<asp:SessionParameterName="interest"SessionField="currinterest"/>

</SelectParameters>

<InsertParameters>

<asp:ParameterName="memberid"Type="Object"/>

<asp:ParameterName="Interest"Type="String"/>

</InsertParameters>

</asp:SqlDataSource>

=============================

Type="Object"|||

Motley wrote:

Type="Object"

Thanks. I assume you are referring to the fact that the "Type='Object'" has to be removed. I just found a reference to that bug. That cleared the problem.

sql

Wednesday, March 21, 2012

Problem with Grid View

i seem to have problems with my grid view not displaying anything when if i test my SQLDataSource, it shows me rows of data.

i seem to have this problem only with my Stored procedure, or when i change my Database structure, like add fields

Here is my procedure: can i do this? select a row and also return a varchar?

ALTER PROCEDURE dbo.SelectUpload(@.FileNamevarchar(50),@.Return varchar(MAX) =NULLOUTPUT)ASSELECT *FROM UploadsWHERE FileName = @.FileNameRETURNSELECT WebPathFROM UploadsWHERE FileName = @.FileName

Assuming that Webpath is a column in Uploads, why do you need the two select statements? When you do Select * from Uploads, Webpath will part of the dataset that the sqldatasource returns. Why can't you do:

ALTER PROCEDURE dbo.SelectUpload

@.FileNamevarchar(50)

AS
SELECT *FROM Uploads
WHERE FileName = @.FileName
RETURN

|||

i was thinking of returning a table to display in a Grid View, and also a Web Path (eg. "~/Images/image.jpg") to be displayed as a image, something like that. so that i don't have to retrieve it from the Data Source or Grid View.

|||

I still think you can grab all the data you need using one select statement and then do what you need to do with the data:

For the data you want displayed in the gridview:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="value1" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="value1" HeaderText="value1" ReadOnly="True" SortExpression="Value1" />
<asp:BoundField DataField="value2" HeaderText="value2" SortExpression="value2" />
<asp:BoundField DataField="value3" HeaderText="value3" SortExpression="value3" />
</Columns>
</asp:GridView>

For the webpath (lets say you want to display in a label):

Dim dv As DataView = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
Dim dr As DataRow
dr = dv.Table.Rows(0)
lblWebPath.Text = dr("WebPath").ToString()