sir i have using asp.net to create login form by using c# language. i have created one table i,e Registration table. in that username and password are the atributes. the problem is while am going to register new user the given data is not inserted into table and while am going to login i got one exception i.e "SqlException was unhandled by user code" in BL.cs file (line con.Open();). please send the correct formate.


BL.cs (this file is saved in App_Code folder).

using System;
using System.Data;
using System.Configuration;
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.Data.Sql;
using System.Data.SqlClient;
namespace Login
{
    /// <summary>
    /// Summary description for BL
    /// </summary>
    public class BL
    {
        SqlConnection con = null;
        SqlCommand cmd = null;
        public BL()
        {
            con = new SqlConnection("server=;database=MailServer");
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;

        }
        public void Save(string username, string password)
        {

            cmd.CommandText = string.Format("insert into UserAccount(username,password) values('{0}','{1}')", username, password);
            cmd.ExecuteNonQuery();
        }
        public bool IsValidUser(string username, string password)
        {
            cmd.CommandText = string.Format("select count(*) from useracount where username='{0}' and password='{1}'", username, password);
            return int.Parse(cmd.ExecuteScalar().ToString()) == 0 ? false : true;
        }
    }
}

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
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 Login;

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

        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            BL db = new BL();
            if (db.IsValidUser(TxtUsername.Text, Txtpassword.Text))
                Response.Redirect("home.aspx");
            else
                msg.InnerHtml = "invalid username and password";

        }
    }

Registration.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.Sql;
using System.Data.SqlClient;
using Login;


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

        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                BL db = new BL();
                db.Save(TxtUsername.Text, Txtpassword.Text);
            }
            catch (SqlException ex)
            {
                msg.InnerHtml = "User already exists";

            }
        }
    }

A SQL connection string normally looks like this:
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

I'm guessing you're failing at the point of opening the connection because the connection string is incorrect. You could always handle the exception to see what is really going on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.