我想知道是否有人可以帮助我解决这个问题。
基本上我有一个 Web 应用程序,它根据 UID 在数据库中搜索一个人。找到名称后,我打开另一个 db conn 并搜索他们的经理电子邮件地址。
但是我收到“对象引用未设置为实例”错误,我假设某些内容为空并且它不喜欢它?那是正确的。
这是我的代码。
public partial class Leaver : System.Web.UI.Page
{
string Managers_Name = null;
string Managers_Email = null;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_SearchDB(object sender, EventArgs e)
{
SqlDataReader reader = null;
SqlConnection conn = null;
try
{
conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["App_NewStarterConnectionString"].ConnectionString);
{
conn.Open();
using (SqlCommand cmd =
new SqlCommand("SELECT * FROM dbo.tb_starters WHERE Payrol = @Payrol", conn))
{
cmd.Parameters.AddWithValue("@Payrol", Payrol.Value);
reader = cmd.ExecuteReader();
while (reader.Read())
{
Fname.Value = reader["FirstName"].ToString();
Lname.Value = reader["LastName"].ToString();
Payrol.Value = reader["Payrol"].ToString();
section.Value = reader["Section"].ToString();
Managers_Name = reader["Manager"].ToString();
}
}
}
}
catch (Exception ee)
{
throw ee;
}
finally {
GetManagersEmail();
if (reader != null)
reader.Close();
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
protected void GetManagersEmail()
{
SqlDataReader reader_new = null;
SqlConnection conn_new = null;
try
{
conn_new = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["App_NewStarterConnectionString"].ConnectionString);
{
conn_new.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Email FROM dbo.tb_starters WHERE FullName = @ManagersName", conn_new))
{
cmd.Parameters.AddWithValue("@ManagersName", Managers_Name);
while (reader_new.Read())
{
Managers_Email = reader_new["Email"].ToString();
Response.Write(Managers_Email);
}
}
}
}
catch (Exception ee)
{
throw ee;
}
finally
{
if (reader_new != null)
reader_new.Close();
if (conn_new.State == ConnectionState.Open)
conn_new.Close();
}
}