3
Answers

How to get multiple column values from DB in session variabl

safal agrawal

safal agrawal

8y
268
1
I want to get UID, username, city, email from registration table (sql server) from DB on successful login of user.<br /> I want to store this values in session variables.<br /> <br /> <br /> plz help me with the code and solution<br /><div> below is my code</div><div>&nbsp;</div><div>protected void ValidateUser(object sender, EventArgs e)<br /> {<br /> int userId = 0;<br /> string constr = ConfigurationManager.ConnectionStrings[&amp;quot;constr&amp;quot;].ConnectionString;<br /> using (SqlConnection con = new SqlConnection(constr))<br /> {<br /> using (SqlCommand cmd = new SqlCommand(&amp;quot;Validate_User&amp;quot;))<br /> {<br /> cmd.CommandType = CommandType.StoredProcedure;<br /> cmd.Parameters.AddWithValue(&amp;quot;@Username&amp;quot;, txtUname.Text.Trim());<br /> cmd.Parameters.AddWithValue(&amp;quot;@Password&amp;quot;, txtPassword.Text.Trim());<br /> cmd.Connection = con;<br /> con.Open();<br /> SqlDataAdapter sda = new SqlDataAdapter(cmd);<br /> DataTable dt = new DataTable();<br /> sda.Fill(dt);<br /> userId = Convert.ToInt32(cmd.ExecuteScalar());<br /> con.Close();<br /> if (dt.Rows.Count &amp;gt; 0)<br /> {<br /> Session[&amp;quot;uid&amp;quot;] = dt.Columns[0].ToString();<br /> Session[&amp;quot;Username&amp;quot;] = dt.Columns[1].ToString();<br /> Session[&amp;quot;city&amp;quot;] = dt.Columns[2].ToString();<br /> Response.Redirect(&amp;quot;Redirectform.aspx&amp;quot;);<br /> Session.RemoveAll();<br /> }<br /> else<br /> {<br /> lblmsg.Text = &amp;quot;You&#39;re username and word is incorrect&amp;quot;;<br /> lblmsg.ForeColor = System.Drawing.Color.Red;<br /> <br /> } <br /> }<br /> switch (userId)<br /> {<br /> case -1:<br /> lblmsg.Text = &amp;quot;Username and/or password is incorrect.&amp;quot;;<br /> break;<br /> case -2:<br /> lblmsg.Text = &amp;quot;Account has not been activated.&amp;quot;;<br /> break;<br /> default:<br /> //FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);<br /> break;<br /> }<br /> }<br /> }<br /> <br /> <br /> This is my stored Procedure..validated user<br /> <br /> ALTER PROCEDURE [dbo].[Validate_User]<br /> @Username NVARCHAR(20),<br /> @Password NVARCHAR(20)<br /> AS<br /> BEGIN<br /> SET NOCOUNT ON;<br /> DECLARE @UserId INT, @LastLoginDate DATETIME<br /> <br /> SELECT @UserId = UserId, @LastLoginDate = LastLoginDate<br /> FROM Users WHERE Username = @Username AND [Password] = @Password<br /> <br /> IF @UserId IS NOT NULL<br /> BEGIN<br /> IF NOT EXISTS(SELECT UserId FROM UserActivation WHERE UserId = @UserId)<br /> BEGIN<br /> UPDATE Users<br /> SET LastLoginDate = GETDATE()<br /> WHERE UserId = @UserId<br /> SELECT @UserId [UserId] -- User Valid<br /> END<br /> ELSE<br /> BEGIN<br /> SELECT -2 -- User not activated.<br /> END<br /> END<br /> ELSE<br /> BEGIN<br /> SELECT -1 -- User invalid.<br /> END<br /> END &nbsp;</div><div>&nbsp;</div>
Answers (3)