Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im providing the code..Please solve the error. Waiting for the solution.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class Default_login_trial : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
         DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter();    
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
            SqlCommand cmd = new SqlCommand("Login_Check_Sp", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@username", txtUserName.Text.Trim());
            cmd.Parameters.AddWithValue("@pwd", txtPwd.Text.Trim());
            adp.SelectCommand = cmd;          
            adp.Fill(dt);
            cmd.Dispose();
            if (dt.Rows.Count > 0)
            {
                lblStatus.Text = "Login Successfull";
                
            }
            else
            {
                lblStatus.Text = "Wrong Username/Password";
                
            }  
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Oops!! following error occured : " + ex.Message.ToString() + "');", true);
        
        }
        finally
        {
            dt.Clear();
            dt.Dispose();
            adp.Dispose();         
        }     
    }
}
Posted
Updated 11-Dec-14 20:47pm
v2
Comments
syed shanu 12-Dec-14 1:52am    
Check with break point and tell us in which line do you get the error
syed shanu 12-Dec-14 1:56am    
Check my article realted to your program.
http://www.codeproject.com/Articles/852205/Insert-select-update-delete-in-asp-net-with-Simple
Member 11305027 12-Dec-14 2:10am    
@syed Shanu : Sircan u please let me know wats the solution for my question.?
syed shanu 12-Dec-14 2:16am    
Chk my solution hope that will help you
Rajesh waran 12-Dec-14 2:45am    
Make a break point and debug your code then find where you are struck with that "msg" and tell us .bcoz we can't find your screen.

1 solution

Try like this

C#
protected void btnLogin_Click(object sender, EventArgs e)
  {

      DataSet ds = new DataSet();
      DataTable dt = new DataTable();
      SqlDataAdapter adp = new SqlDataAdapter();

      using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
      {
          SqlCommand cmd = new SqlCommand("Login_Check_Sp", conn);
          cmd.Parameters.AddWithValue("@username", txtUserName.Text.Trim());
          cmd.Parameters.AddWithValue("@pwd", txtPwd.Text.Trim());

          cmd.CommandType = CommandType.StoredProcedure;

          SqlDataAdapter da = new SqlDataAdapter();
          da.SelectCommand = cmd;
          da.Fill(ds);
          dt = ds.Tables[0];

          if (dt.Rows.Count > 0)
          {
              lblStatus.Text = "Login Successfull";

          }
          else
          {
              lblStatus.Text = "Wrong Username/Password";

          }
      }



  }


Try the above code and still if you have error .
Debug aand using break point check where you get the error.
There might be many reason as connection might not established.
here is few link which explains how to use break point in visual studio.

http://www.tutorialspoint.com/asp.net/asp.net_debugging.htm[^]

https://www.youtube.com/watch?v=k1EQndVmlOc[^]
 
Share this answer
 
v2
Comments
Member 11305027 12-Dec-14 2:25am    
Sir this is also nt working.
syed shanu 12-Dec-14 2:26am    
I msg you to check with break point and in which line do you get the error.
Member 11305027 12-Dec-14 2:27am    
Can u tell me how to use breakpoint in the program as i have never use breakpoint.??
syed shanu 12-Dec-14 2:43am    
I have updated my solution check it
Member 11305027 12-Dec-14 4:37am    
I tried the code dat u gave me.then also error coming.The error coming is in the line
da.Fill(ds);
as SQLException was unhandled by user code. Could not find stored procedure 'Login_Check_Sp'.


Could not find stored procedure 'Login_Check_Sp'.

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: Could not find stored procedure 'Login_Check_Sp'.

Source Error:


Line 32: SqlDataAdapter da = new SqlDataAdapter();
Line 33: da.SelectCommand = cmd;
Line 34: da.Fill(ds);
Line 35: dt = ds.Tables[0];
Line 36:

The Stored procedure i wrote is as follows :

CREATE PROCEDURE Login_Check_Sp
-- Add the parameters for the stored procedure here
@username varchar(100),
@pwd varchar(100)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
where UserName COLLATE Latin1_general_CS_AS=@username
and [Password] COLLATE Latin1_general_CS_AS=@pwd
-- Insert statements for procedure here
SELECT * FROM Login_Tb WHERE Username =@username AND password =@pwd
END
GO

Please solve the error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900