如何在ASP.NET中的动态标签中添加数据库值 [英] How do I add database value in dynamic label in ASP.NET

查看:130
本文介绍了如何在ASP.NET中的动态标签中添加数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的对象引用未设置为此代码中的对象实例



我尝试过的内容:



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System。 Data.SqlClient;

使用System.Configuration;

使用System.Data;



命名空间AttendanceSystem

{

公共部分类测试:System.Web.UI.Page

{

protected void Page_Load(object sender ,EventArgs e)

{

string dropddbranch = Session [Depart]。ToString();

string dropddsection = Session [Section ] .ToString();

string Connectionstring = ConfigurationManager.ConnectionStrings [connstr]。ToString();

SqlConnection objConnec tion = new SqlConnection(Connectionstring);

objConnection.Open();

SqlCommand cmd = new SqlCommand(从Student_table中选择count(Student_name),其中Section_id ='+ dropddsection +'和S_Department ='+ dropddbranch +',objConnection);

cmd.ExecuteNonQuery();

int no =(int)cmd.ExecuteScalar ();

int i = 0;

/ * Label label = new Label();

label.Text = no.ToString( );

Form.Controls.Add(label); * /

// Label label = new Label();

Label [] label = new Label [no];

SqlCommand mycommand = new SqlCommand(从Student_table中选择Roll_no,其中Section_id ='+ dropddsection +'和S_Department ='+ dropddbranch +'按Roll_no排序asc,objConnection);

SqlDataReader myReader = mycommand.ExecuteReader();
while(myReader.Read()&&我< no)

{



// TxtRegNo.Text =(myReader [Reg_no]。ToString());

// myReader.Read();

string lb = myReader [Roll_no]。ToString();

label [i] .Text = lb;

Form.Controls.Add(label [i]);

i ++;

}

objConnection.Close ();

}





}

}

i am having object reference not set to an instance of an object in this code

What I have tried:

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

namespace AttendanceSystem
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string dropddbranch = Session["Depart"].ToString();
string dropddsection = Session["Section"].ToString();
string Connectionstring = ConfigurationManager.ConnectionStrings["connstr"].ToString();
SqlConnection objConnection = new SqlConnection(Connectionstring);
objConnection.Open();
SqlCommand cmd = new SqlCommand("select count(Student_name) from Student_table where Section_id= '" + dropddsection + "' and S_Department= '" + dropddbranch + "'", objConnection);
cmd.ExecuteNonQuery();
int no = (int)cmd.ExecuteScalar();
int i = 0;
/*Label label = new Label();
label.Text = no.ToString();
Form.Controls.Add(label);*/
// Label label = new Label();
Label[] label= new Label[no];
SqlCommand mycommand = new SqlCommand("select Roll_no from Student_table where Section_id= '" + dropddsection + "' and S_Department= '" + dropddbranch + "' order by Roll_no asc", objConnection);
SqlDataReader myReader = mycommand.ExecuteReader();
while(myReader.Read() && i<no)
{

// TxtRegNo.Text = (myReader["Reg_no"].ToString());
// myReader.Read();
string lb = myReader["Roll_no"].ToString();
label[i].Text = lb;
Form.Controls.Add(label[i]);
i++;
}
objConnection.Close();
}


}
}

推荐答案

错误可能是由于您的Session变量为空。我建议你在访问Session值之前检查null。例如:



The error was probably caused by your Session variable being null. I would suggest you to check for null before accessing Session values. For example:

if(Session["Depart"] != null && Session["Section"] != null){
   //your code here
   //acces session values here
}





此外,将值直接附加到SQL语句字符串是一个很大的否定因为它可能导致SQL注入攻击。习惯使用参数化查询。其次,养成将使用SqlConnection,SqlCommand和SqlDataAdapter等资源的对象放在using语句中的习惯,以确保在使用对象后正确处理和关闭对象。



Also, appending the values directly to your SQL statement string is a big No No as it can lead you to SQL Injection attack. Make it a habit to use parameterize query. Second, make it a habit to put objects that eat resources such as SqlConnection, SqlCommand and SqlDataAdapter within a using Statement to ensure that objects will be properly disposed and closed after they are used.


< b>由于未处理null值,因此未将对象引用设置为对象的实例问题。



Ex。如果任何变量再次包含null值,试图将该值转换为此对象引用问题发生时的任何其他值。

在您的情况下,您尝试将会话值转换为字符串。

会话[离开] .ToString()



因此会话值可能为空。



在这种情况下,如果你想处理空值,你可以在将值转换为字符串之前进行空值检查。

喜欢

如果(Session [Depart]!= null)

{

//然后你的代码。

}



或者使用处理空值的Convert.Tostring(Session [Depart])。
Object reference not set to an instance of an object issue occurred due to not handle null value.

Ex. If any variable contains null value again trying to convert that value to any other value that time this object reference issue occur.
In your case you are trying to convert the session value to string.
Session["Depart"].ToString().

So it may the session value is null.

Fro this scenario if you want to handle null value you can null check before converting the value to string.
like
If (Session["Depart"]!=null)
{
// then your code.
}

Or use Convert.Tostring(Session["Depart"]) which handle null value.


这篇关于如何在ASP.NET中的动态标签中添加数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆