有系统问题.NullReferenceException错误...请帮助 [英] have problem with system.NullReferenceException error ...please help

查看:90
本文介绍了有系统问题.NullReferenceException错误...请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的错误就像

错误:System.NullReferenceException:对象引用未设置为对象的实例。 at student._Default.clicked(Object sender,EventArgs e)

不知道为什么?我的代码有什么问题吗?

请帮忙...



thnks ...







 使用系统; 
使用 System.Collections;
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Data.SqlClient;

命名空间学生
{
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
}

受保护 void 点击( object sender,EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ stdConnectionStrings]的ConnectionString)。
con.Open();
string user = select count(* )来自stdtable,其中std_name =' + TextBox1.Text + ';
SqlCommand com = new SqlCommand(user,con);
int a = Convert.ToInt32(com.ExecuteScalar());
if (a == 1
{
回复。写( 用户已存在);
}
string insertQuery = 插入stdtable(std_name,roll,std_address)值(@ std_name,@ roll,@ std_address);
com.Parameters.AddWithValue( @ std_name,TextBox1.Text);
com.Parameters.AddWithValue( roll,TextBox2.Text);
com.Parameters.AddWithValue( @ std_address,TextBox3.Text);
com.ExecuteNonQuery();
Response.Redirect( stdadmin.aspx);
Response.Write( registration successcescesfull ...);
con.Close();
}
catch (例外情况)
{
Response.Write( 错误: + ex.ToString());
}
}
}
}

解决方案

ConfigurationManager.ConnectionStrings [stdConnectionStrings]。ConnectionString

检查这是否包含值。



如果你调试代码,你将能够到达抛出错误的确切行。


检查配置文件中的连接字符串名称是否正确。如果您的连接字符串名称不正确,您将收到此错误。


这是什么意思?



SqlConnection con =

新的SqlConnection

(ConfigurationManager.ConnectionStrings

[stdConnectionStrings] .ConnectionString);

con.Open ();

hi,I'm getting error like
error:System.NullReferenceException: Object reference not set to an instance of an object. at student._Default.clicked(Object sender, EventArgs e)
don't know why?is there anything wrong in my code?
please help...

thnks...



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;

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

        protected void clicked(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["stdConnectionStrings"].ConnectionString);
                con.Open();
                string user = "select count(*)from the stdtable where std_name='" + TextBox1.Text + "'";
                SqlCommand com = new SqlCommand(user, con);
                int a = Convert.ToInt32(com.ExecuteScalar());
                if (a == 1)
                {
                    Response.Write("user already exist");
                }
                string insertQuery = "insert into stdtable (std_name,roll,std_address) values (@std_name,@roll,@std_address)";
                com.Parameters.AddWithValue("@std_name", TextBox1.Text);
                com.Parameters.AddWithValue("roll", TextBox2.Text);
                com.Parameters.AddWithValue("@std_address", TextBox3.Text);
                com.ExecuteNonQuery();
                Response.Redirect("stdadmin.aspx");
                Response.Write("registration succescesfull...");
                con.Close();
            }
            catch (Exception ex)
            {
                Response.Write("error: " + ex.ToString());
            }
        }
    }
}

解决方案

ConfigurationManager.ConnectionStrings["stdConnectionStrings"].ConnectionString
Check if this contains a value.

If you debug your code you will be able to get to the exact line throwing the error.


Check if your connection string name is correct in your config file. If you have your connection string name incorrect, you will get this error.


what does it mean?

SqlConnection con =
new SqlConnection
(ConfigurationManager.ConnectionStrings
[ "stdConnectionStrings" ].ConnectionString);
con.Open();


这篇关于有系统问题.NullReferenceException错误...请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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