Hi,
i'm new to this field.Now i'm trying to create login form with MySql as DB.I've following error.
Can anyone help me?
On SubmitButton_Click onclick event:
string constr = "server=localhost;user name=root;password=pwd;database=contacts";
MySqlConnection con = new MySqlConnection(constr);
query = "SELECT Count(*) FROM contacts WHERE username='" + nametxt.Text + "' AND password='" + pwdtxt.Text + "'";
MySqlCommand com = new MySqlCommand(query, con);
con.Open();
string result = string.Empty;
int aa = Convert.ToInt32(com.ExecuteScalar().ToString());
con.Close();
if(aa==0)
{
lblerr.Text = "Invalid username or password";
}
else
{
Response.Redirect("detailadd.aspx");
}
error:
unhandled exception by user
Thanks in advance

Satyapriya NayakPosted Apr 28, 2013, 5:10 AM
http://dev.mysql.com/downloads/connector/net/6.1.html
Open Visual Studio and click on the new project and give the project a name; it will open the new project, then click on the solution explorer (F4); right-click on "Reference" to add a new reference into the project. Add a reference for those two .dll files to the project (MySql.dll (Win apps), MySql.web.dll (Web apps)).
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Mysql_login_page._Default" %>
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.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
namespace Mysql_login_page
{
public partial class _Default : System.Web.UI.Page
{
string strcon = "Server=localhost;Database=test;Uid=root;Pwd=;";
string str;
MySqlCommand com ;
object obj;
protected void btn_login_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(strcon);
con.Open();
str = "select count(*) from login where UserName=@UserName and Password =@Password";
com = new MySqlCommand(str, con);
com.CommandType = CommandType.Text;
com.Parameters.AddWithValue("@UserName", TextBox_user_name.Text);
com.Parameters.AddWithValue("@Password", TextBox_password.Text);
obj = com.ExecuteScalar();
if (Convert.ToInt32(obj) != 0)
{
Response.Redirect("detailadd.aspx");
}
else
{
lb1.Text = "invalid user name and password";
}
con.Close();
}
}
}
Kavi sujaPosted Apr 29, 2013, 6:35 AM
Thanks for your suggestion.Now it works clearly... I use login control here.
using System;
Kavi sujaPosted Apr 29, 2013, 6:34 AM
Jignesh TrivediPosted Apr 28, 2013, 11:49 PM
hi,
you are getting "unhandled exception by user",
so put Try catch block and trying to catch exception.
also try to find out which line of your code throw error? and share with us.