我得到错误“对象引用未设置为对象的实例”当我运行此代码时,L可以做什么来摆脱这个。 [英] I Am Getting The Error "Object Reference Not Set To An Instance Of An Object" While I Run This Code, L What Can Be Done To Get Rid Of This.

查看:63
本文介绍了我得到错误“对象引用未设置为对象的实例”当我运行此代码时,L可以做什么来摆脱这个。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CS代码



使用系统;

使用System.Collections .Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;



公共部分类WebForm:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{



}

protected void Button1_Click(object sender,EventArgs e)

{

ConnectionClass obj = new ConnectionClass();

obj.Connection();

obj.cmd.CommandText =sp_getstudentdata;

obj.con.Open();

obj.dr = obj.cmd.ExecuteReader ();

if(obj.dr.HasRows)

{

this.GridView1.DataSource = obj.dr;

this.GridView1.DataBind();

}

else

{

Response.Write(未找到数据);

}

obj.dr.Close( );

obj.con.Close();



}

}



HTML CODE







<!DOCTYPE html>



< html xmlns =http://www.w3.org/1999/xhtml>

< head runat =server>

< title>< / title>

< / head>

< body>

< form id =form1runat =server>

< div>



< asp:TextBox ID =TextBox1runat =server>< / asp:TextBox>

& nbsp;< asp:Button ID = Button1runat =serverOnClick =Button1_ClickText =Button/>

< br />

< br />

< asp:GridView ID =GridView1runat =serverCellPadding =4ForeColor =#333333GridLines =None>

< AlternatingRowStyle BackColor =WhiteForeColor =#284775/>

< EditRowStyle BackColor =#999999/>

< FooterStyle BackColor =#5D7B9DFont-Bold =TrueForeColor =White/>

< HeaderStyle BackColor =#5D7B9DFont-Bold =True ForeColor =白色/>

< PagerStyle BackColor =#284775ForeColor =WhiteHorizo​​ntalAlign =Center/>

< RowStyle BackColor =#F7F6F3ForeColor =#333333/>

< SelectedRowStyle BackColor =#E2DED6Font-Bold =TrueForeColor =#333333/>

< SortedAscendingCellStyle BackColor =#E9E7E2/>

< SortedAscendingHeaderStyle BackColor =#506C8C/>

< SortedDescendingCellStyle BackColor = #FFFDF8/>

< SortedDescendingHeaderStyle BackColor =#6F8DAE/>

< / asp:G ridView>



< / div>

< / form>

< / body>

< / html>



连接类



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Data;

使用System.Data.SqlClient;



///< summary>

/// ConnectionClass的摘要说明

///

公共类ConnectionClass

{

public SqlConnection con;

public SqlCommand cmd;

public SqlDataReader dr;



public void Connection( )

{

SqlConnection con = new SqlConnection(Data source = ASHOK-PC; integrated security = true; initial catalog = db);

SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.StoredProcedure;

}







}

CS Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

}
protected void Button1_Click(object sender, EventArgs e)
{
ConnectionClass obj = new ConnectionClass();
obj.Connection();
obj.cmd.CommandText = "sp_getstudentdata";
obj.con.Open();
obj.dr = obj.cmd.ExecuteReader();
if (obj.dr.HasRows)
{
this.GridView1.DataSource = obj.dr;
this.GridView1.DataBind();
}
else
{
Response.Write("No Data Found");
}
obj.dr.Close();
obj.con.Close();

}
}

HTML CODE



<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

</div>
</form>
</body>
</html>

Connection Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ConnectionClass
///
public class ConnectionClass
{
public SqlConnection con;
public SqlCommand cmd;
public SqlDataReader dr;

public void Connection()
{
SqlConnection con = new SqlConnection("Data source=ASHOK-PC;integrated security=true;initial catalog=db");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
}



}

推荐答案

修改sqlconnection。也可以设置db的用户名和密码
modify the sqlconnection .also set the username and password of your db


这篇关于我得到错误“对象引用未设置为对象的实例”当我运行此代码时,L可以做什么来摆脱这个。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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