Monday, March 26, 2012

please help

Hi! I'm new at asp .net programming, and would appreciate a littlehelp with a problem I have. I am trying to create a social network, andthe aim of this specific VB page is to show all of the friends of theuser who is logged in. I would like to do this without using gridviews,so that I have more freedom in the way the results of the select areshown and formatted. I found some code which is perfect for anotherpage where I simply show all the users in the database. However, forthis page where I show the friends of the logged in user, I am havingdifficulties because I need to use a parameter @.Param1 which passes thevalue of the username of who is logged in. Previously I was doing thiswith gridviews, and the passing of the parameter was easy, since on thepagepreinit I could declare Session("username") = User.Identity.Nameand then simply use username inside the select command of thesqldatasource ( <asp:SessionParameter Name="Param1"SessionField="username" />)

However, now without the gridview I don't know how I can continue using this parameter. Any suggestions?
Thanks a lot!

Here is the VB code:

<%@. Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Data.SqlClient" %
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"
<script language="VB" runat="server">
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
Page.Theme = Profile.MyTheme
Session("username") = User.Identity.Name

End Sub
Sub Page_Load(Sender As Object, E As EventArgs)

Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As New SqlDataAdapter


' MyCommand.SelectCommand.Parameters.AddWithValue("@.Param1", "")
' not sure about the above line, something is missing


MyConnection = New SqlConnection("DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;IntegratedSecurity=True;User Instance=True")
MyCommand = NewSqlDataAdapter("select c.UserName from aspnet_Users a inner joinaspnet_friendship b on (a.userId = b.userId or a.userId = b.buddyId)inner join aspnet_Users c on (b.userId = c.userId or b.buddyId =c.userId) where a.UserName = @.Param1 and c.UserName <>@.Param1", MyConnection)
DS = New DataSet()
MyCommand.Fill(DS, "aspnet_Users")

MyDataList.DataSource = DS.Tables("aspnet_Users").DefaultView
MyDataList.DataBind()
End Sub

</script
<body
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox
<ASP:DataList id="MyDataList" RepeatColumns="1" runat="server"
<ItemTemplate
<table cellpadding=10 style="font: 10pt verdana">
<tr>
<td width=1 bgcolor="BD8672"/
<td valign="top">

</td
<td valign="top"
<b>Name: </b><%#DataBinder.Eval(Container.DataItem, "UserName")%><br>
<b>Photo: </b><%#DataBinder.Eval(Container.DataItem, "UserName")%><br>
<p
<a href='<%# DataBinder.Eval(Container.DataItem, "UserName", "purchase.aspx?titleid={0}") %>' >
<img border="0" src="http://pics.10026.com/?src=/quickstart/aspplus/images/purchase_book.gif" >
</a
</td>
</tr>
</table
</ItemTemplate
</ASP:DataList

</body
</asp:Content>

This should do what you are looking for

Sub Page_Load(Sender As Object, E As EventArgs)

Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter


MyConnection = New SqlConnection("DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;IntegratedSecurity=True;User Instance=True")
MyCommand = NewSqlDataAdapter("select c.UserName from aspnet_Users a inner joinaspnet_friendship b on (a.userId = b.userId or a.userId = b.buddyId)inner join aspnet_Users c on (b.userId = c.userId or b.buddyId =c.userId) where a.UserName = @.Param1 and c.UserName <>@.Param1", MyConnection)

MyCommand.SelectCommand.Parameters.AddWithValue("@.Param1", Session("username"))


DS = New DataSet()
MyCommand.Fill(DS, "aspnet_Users")

MyDataList.DataSource = DS.Tables("aspnet_Users").DefaultView
MyDataList.DataBind()
End Sub

|||

Thanks for the help. Now I get an error in the line:

MyCommand.Fill(DS, "aspnet_Users")

The error is: "The parameterized query '(@.Param1 nvarchar(4000))select c.UserName fromaspnet_Users a in' expects the parameter '@.Param1', which was notsupplied."

I am not sure what I should include inside the MyCommand.Fill in order to show the results that I want, considering what I posted before. I would like to show the friends of the logged in user.
I'm sorry I have to ask all this, but I am relatively new at this and would appreciate any help :)

Thanks.


|||

Hi!

I managed to solve the problem :)

The final code is:

<script language="VB" runat="server">
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
Page.Theme = Profile.MyTheme
Session("username") = User.Identity.Name
End Sub
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter


MyConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True")

MyCommand = New SqlDataAdapter("select c.UserName from aspnet_Users a inner join aspnet_friendship b on (a.userId = b.userId or a.userId = b.buddyId) inner join aspnet_Users c on (b.userId = c.userId or b.buddyId = c.userId) where a.UserName = @.Param1 and c.UserName <> @.Param1", MyConnection)

MyCommand.SelectCommand.Parameters.AddWithValue("@.Param1", Session("username"))

DS = New DataSet()
MyCommand.Fill(DS, "aspnet_friendship")

MyDataList.DataSource = DS.Tables("aspnet_friendship").DefaultView
MyDataList.DataBind()
End Sub
</script>

Thanks once again.

No comments:

Post a Comment