HOME > .NET Framework > Forum > ช่วยดูโค้ดหน้าล็อกอินทีค่ะ พอลองล็อกอินที่ไม่มีในฐานข้อมูลก็ขึ้นฟ้อง Object reference not set to an instance of an object. อ่ะ
ช่วยดูโค้ดหน้าล็อกอินทีค่ะ พอลองล็อกอินที่ไม่มีในฐานข้อมูลก็ขึ้นฟ้อง Object reference not set to an instance of an object. อ่ะ
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 System.Data.SqlClient;
using System.Web.Configuration;
using System.IO;
using System.Text;
public partial class login : System.Web.UI.Page
{
string strConn = WebConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
SqlConnection myConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
string sqlSelect;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
sqlSelect = " SELECT StudentUser,StudentPsw FROM Tb_Student WHERE (Tb_Student.StudentUser='" + txtUserName.Text + "')";
sqlSelect += " AND (Tb_Student.StudentPsw ='" + txtPassWord.Text + "')";
SqlDataAdapter adap = new SqlDataAdapter(sqlSelect, myConn);
DataSet ds = new DataSet();
adap.Fill(ds, "data");
int count = ds.Tables["data"].Rows.Count;
string[] user = new string[count];
string[] pass = new string[count];
if (count == 0)
{
lblAlert.Text = "ไม่พบข้อมูลในฐานข้อมูล";
Session.RemoveAll();
}
if (count != 0)
{
for (int i = 0; i <= count - 1; i++)
{
user[i] = ds.Tables["data"].Rows[i].ItemArray[1].ToString();
// เก็บ session
Session["user"] = user[i];
Session["Login"] = "ok";
}
}
Response.Redirect("StuMain.aspx");
}
}
พอลองล็อกอินที่ไม่มีในฐานข้อมูลก็ขึ้นฟ้อง Object reference not set to an instance of an object. อ่ะ
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 System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web.Configuration;
public partial class StuProfile : System.Web.UI.Page
{
string strConn = WebConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
SqlConnection myConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lblUser.Text = Session["user"].ToString();
ShowData();
}
}
protected void ShowData()
{
string sqlSelect;
sqlSelect = "SELECT * FROM Tb_Student WHERE (Tb_Student.StudentNM = ' " + lblUser.Text + " ')";
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
myConn.ConnectionString = strConn;
myConn.Open();
}
SqlDataAdapter da = new SqlDataAdapter(sqlSelect, myConn);
DataSet ds = new DataSet();
da.Fill(ds, "data");
int count = ds.Tables["data"].Rows.Count;
string[] id = new string[count];
string[] sName = new string[count];
if (count == 0)
{
Label lbl = new Label();
lbl.Text = "<script>alert('ไม่พบ Username และ Password ในฐานข้อมูล')</script>";
Page.Controls.Add(lbl);
}
if (count != 0)
{
for (int i = 0; i <= count - 1; i++)
{
id[i] = ds.Tables["data"].Rows[i].ItemArray[0].ToString();
sName[i] = ds.Tables["data"].Rows[i].ItemArray[1].ToString();