My goal is to have the user login and then send them to a members page based on their role and then show infomation about the logged in user based on the UserName. The problem I am having is correctly passing the parameter value into the sql SelectCommand
Where parameter. Unless I hard code it with a DefaultValue statement I get no result (Null Value passed) I am new to asp.net and have spent about 30 hours of reading and searching with no success. Here is the code using asp.net 4.5. Can this not be done this
way?
ohgrrr
0 Points
3 Posts
Parameter passing problem asp sql vb code
May 02, 2012 01:30 PM|LINK
My goal is to have the user login and then send them to a members page based on their role and then show infomation about the logged in user based on the UserName. The problem I am having is correctly passing the parameter value into the sql SelectCommand Where parameter. Unless I hard code it with a DefaultValue statement I get no result (Null Value passed) I am new to asp.net and have spent about 30 hours of reading and searching with no success. Here is the code using asp.net 4.5. Can this not be done this way?
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [UserId], [First Name] AS First_Name, [Last Name] AS Last_Name, [City] FROM [Users] WHERE ([UserName] = @strUserName)">
<SelectParameters>
<asp:Parameter Name="strUserName" Type="String"/>
</SelectParameters>
</asp:SqlDataSource>
--------VB code---------
Public Class MemberHome
Inherits System.Web.UI.Page
Public strUserName As String = Membership.GetUser().UserName.ToString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write("Test#1 - " & strUserName)
End Sub
End Class
Sandeep Shen...
Participant
1678 Points
492 Posts
Re: Parameter passing problem asp sql vb code
May 02, 2012 01:34 PM|LINK
HI
Instead of using SQLDATASOURCE...why dont you use stored procedure ??
Skype : sandeep.d.shenoy
Gmail : sandeepdshenoy@gmail.com
ohgrrr
0 Points
3 Posts
Re: Parameter passing problem asp sql vb code
May 02, 2012 02:03 PM|LINK
Where can I find an example of a stored procedure and how it is implemented?
AccuWebHosti...
Contributor
2212 Points
401 Posts
Re: Parameter passing problem asp sql vb code
May 02, 2012 02:29 PM|LINK
Hi!
Here is the code for authenticate user and redirect to home page. You can can see Logged user in the top right corner of each page.
Code behind(*.cs) File
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; public partial class client : System.Web.UI.MasterPage { ConfigClass c = new ConfigClass(); protected void Page_Load(object sender, EventArgs e) { if (Session["user"] != null) { div_login.Visible = false; div_logout.Visible = true; Label1.Text = Convert.ToString(Session["user"]); } else { div_login.Visible = true; div_logout.Visible = false; } } protected void Button1_Click(object sender, EventArgs e) { string query = "Select id,uname,pwd FROM user_det where uname='"+TextBox1.Text+"' and pwd='"+TextBox2.Text+"'"; DataTable dt = c.dtTable(query); if (dt.Rows.Count > 0) { if (Convert.ToString(dt.Rows[0][1]) == TextBox1.Text && Convert.ToString(dt.Rows[0][2]) == TextBox2.Text) { Session["user"] = TextBox1.Text; Response.Redirect("Home.aspx"); } else { System.Windows.Forms.MessageBox.Show("Incurrect UserName or Password...","Authentication Failed"); } } else { System.Windows.Forms.MessageBox.Show("Incurrect UserName or Password...", "Authentication Failed"); } } }Code(Design) File (Write this code in Master Page)
<div id="div_logout" runat="server" style="text-align:right;"> Welcome, <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Logout.aspx">Logout</asp:HyperLink> </div> <div id="div_login" runat="server"> User ID : <asp:TextBox ID="TextBox1" runat="server" Height="15px" CssClass="tb" Width="98px"></asp:TextBox> Password : <asp:TextBox ID="TextBox2" runat="server" Height="15px" CssClass="tb" Width="96px"></asp:TextBox><br /> <br /> <asp:Button ID="Button1" runat="server" Text="Login" CssClass="bt" Height="27px" Width="80px" onclick="Button1_Click" /> <input type="reset" id="rst1" runat="server" class="bt" /> </div>Regards.
Top Windows VPS Provider
ohgrrr
0 Points
3 Posts
Re: Parameter passing problem asp sql vb code
May 03, 2012 01:10 AM|LINK
Thanks Sandeep.
BTW here is the video that explains everything about Stored Procedures
http://www.asp.net/web-forms/videos/sql-2005/creating-and-using-stored-procedures
Check it out as it is awesome!