system.NullReferenceException有问题 [英] have problem with system.NullReferenceException
问题描述
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Data.SqlClient;
命名空间 WindowsFirstsample
{
public partial class Form1:Form
{
private SqlConnection cn;
private SqlDataAdapter da;
private SqlCommand cmd;
private DataSet ds;
private string strcon;
private string strq;
private int icounter;
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender,EventArgs e)
{
strcon = 数据源= HP-PC // SQLEXPRESS;初始目录=学校;综合安全性=真;;
cn = new SqlConnection(strcon);
strq = select * from student;
cn.Open();
da = new SqlDataAdapter(strq,cn);
ds = new DataSet();
da.Fill(ds);
textBox1.Text = ds.Tables [ 0 ]。行[ 0 ] [ id]。ToString();
textBox2.Text = ds.Tables [ 0 ]。行[ 0 ] [ name]。ToString();
}
private void button1_Click( object sender,EventArgs e)
{
icounter = 0 ;
textBox1.Text = ds.Tables [ 0 ]。行[ 0 ] [ id]。ToString();
textBox2.Text = ds.Tables [ 0 ]。行[ 0 ] [ name]。ToString();
}
private void button2_Click(< span class =code-keyword> object sender,EventArgs e)
{
int irowcount = ds.Tables [< span class =code-digit> 0 ]。Rows.Count - 1 ;
if (icounter < irowcount)
{
icounter ++;
textBox1.Text = ds.Tables [ 0 ]。行[icounter] [ < span class =code-string> id]。ToString();
textBox2.Text = ds.Tables [ 0 ]。行[icounter] [ < span class =code-string> name]。ToString();
}
else
{
MessageBox.Show( 您已经在最后一条记录);
}
}
private void button3_Click( object sender,EventArgs e)
{
if (icounter > 0 )
{
icounter--;
textBox1.Text = ds.Tables [ 0 ]。行[icounter] [ < span class =code-string> id]。ToString();
textBox2.Text = ds.Tables [ 0 ]。行[icounter] [ < span class =code-string> name]。ToString();
}
else
{
MessageBox.Show( 您已经在第一条记录);
}
}
private void button4_Click( object sender,EventArgs e)
{
icounter = ds.Tables [ 0 ] .Rows.Count - 1 ;
textBox1.Text = ds.Tables [ 0 ]。行[icounter] [ < span class =code-string> id]。ToString();
textBox2.Text = ds.Tables [ 0 ]。行[icounter] [ < span class =code-string> name]。ToString();
}
}
}
来自msdn:
Quote:尝试取消引用空对象引用时引发的异常。
这里是链接: NullReferenceException [ ^ ]
此异常表示您尝试访问不存在的对象。最好在使用引用之前检查空值。
最好:使用调试器
F5开始调试
F9切换断点
F10跨越
F11步入
祝你有个美好的一天。
im using sqlserver2008,Visual studio 2010 on windows 7 ultimate.i have school database which contains student table.im not able to access the data from the backend.whats wrong in this code.object reference not set to an instance of an object.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFirstsample
{
public partial class Form1 : Form
{
private SqlConnection cn;
private SqlDataAdapter da;
private SqlCommand cmd;
private DataSet ds;
private string strcon;
private string strq;
private int icounter;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
strcon="Data Source=HP-PC//SQLEXPRESS;Initial Catalog=school;Integrated security=true; ";
cn = new SqlConnection(strcon);
strq = "select * from student";
cn.Open();
da = new SqlDataAdapter(strq,cn);
ds = new DataSet();
da.Fill(ds);
textBox1.Text = ds.Tables[0].Rows[0]["id"].ToString();
textBox2.Text = ds.Tables[0].Rows[0]["name"].ToString();
}
private void button1_Click(object sender, EventArgs e)
{
icounter = 0;
textBox1.Text = ds.Tables[0].Rows[0]["id"].ToString();
textBox2.Text = ds.Tables[0].Rows[0]["name"].ToString();
}
private void button2_Click(object sender, EventArgs e)
{
int irowcount = ds.Tables[0].Rows.Count - 1;
if (icounter < irowcount)
{
icounter++;
textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString();
textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString();
}
else
{
MessageBox.Show("you are already on the last record");
}
}
private void button3_Click(object sender, EventArgs e)
{
if (icounter > 0)
{
icounter--;
textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString();
textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString();
}
else
{
MessageBox.Show("you are already on the first record");
}
}
private void button4_Click(object sender, EventArgs e)
{
icounter = ds.Tables[0].Rows.Count - 1;
textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString();
textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString();
}
}
}
from msdn:
Quote:The exception that is thrown when there is an attempt to dereference a null object reference.
here is the link: NullReferenceException[^]
this exception means that you tried to access a non-existing object. it is better to check for null values before using references.
the best: use the debugger
F5 start debug
F9 toggle breakpoint
F10 step over
F11 step into
have a good day.
这篇关于system.NullReferenceException有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!