使用csharp将数据库中的数据显示到gridview中 [英] Shows the data from the database into the gridview using csharp

查看:137
本文介绍了使用csharp将数据库中的数据显示到gridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT S.Marks,S.QPName,B.Class,B.BthNo,B.Batchdate,B.Attender 
FROM STUDDET S,Batch B where S.BthId = B.BthId and  S.StudId = '55334'





当我按如下方式执行上述查询输出时;





when i execute the above query output as follows;

Marks   QpName         Class     Bthno     Batchdate     Attender

56	REO_2011	REO	B120RE	2011-09-26 	NIRANJAN
64	REO_2011	REO	B120RE	2011-09-26 	NIRANJAN







在运行模式下如下;






in run mode as follows;

Student id    txt_studid(textbox)  Show (Button)







当我进入学生ID并点击显示按钮,记录显示在gridview中显示





显示但是ton代码如下;








when i enter the student id and click the show button, the record show be displayed into the gridview


Show Button code as follows;


 private string SQl;
    private SqlDataReader Dr;
    private SqlDataCon SCon = new SqlDataCon();



SQl = "SELECT S.Marks, S.QPName,B.Class,B.BthNo,B.Batchdate,B.Attender FROM STUDDET S,Batch B";
 SQl = SQl + "where S.BthId = B.BthId and  s.StudId = '" + txt_Studid.Text.ToString() + "'";
        try
        {
            Dr = SCon.ReadSql(SQl);
            this.GridView1.DataSource = Dr;
            this.GridView1.DataBind();
            Dr.Close();
        }
        catch (Exception e1)
        {
            Label4.Text = e1.Message.ToString();
            return;
        }



当我在文本框中输入学生ID并单击显示按钮错误显示如下;



对象引用未设置为对象实例。



请帮助我。



我的显示按钮代码有什么问题。



Rgds,

Narasiman P.


when i enter the student id in the textbox and click the Show button error shows as follows;

object reference not set to an instance of object.

please help me.

what is the problem in my Show Button code.

Rgds,
Narasiman P.

推荐答案

先生。 Narasiman,

请检查以下代码,它可能有所帮助:





使用System.Data.SqlClient ;

使用System.Data;



私人字符串SQl;



DataSet ds = new DataSet();

DataTable dt = new DataTable();



SqlConnection con = new SqlConnection(/ * connection string (数据库)* /);

SqlDataAdapter adapter = new SqlDataAdapter();



SQl =SELECT S.Marks,S.QPName,B.Class,B.BthNo,B.Batchdate,B.Attender FROM STUDDET S,Batch B ;

SQl = SQl +其中S.BthId = B.BthId和s.StudId =''+ txt_Studid.Text.ToString()+'';





SqlCommand command = new SqlCommand(SQ1,con);

try

{

adapter.SelectCommand = command;

adapter.Fill(ds,OUTPUT);

dt = ds.Tables { OUTPUT];

adapter.Dispose();

command.Dispose();

this.GridView1.DataSource = dt;

this.GridView1.DataBind();



}

catch(例外e1)

{

Label4.Text = e1.Message.ToString();

返回;

}









试一试并回复
Mr. Narasiman,
Please check the following code, It may help :


using System.Data.SqlClient;
using System.Data;

private string SQl;

DataSet ds = new DataSet();
DataTable dt = new DataTable();

SqlConnection con = new SqlConnection( /* connection string (database) */);
SqlDataAdapter adapter = new SqlDataAdapter();

SQl = "SELECT S.Marks, S.QPName,B.Class,B.BthNo,B.Batchdate,B.Attender FROM STUDDET S,Batch B";
SQl = SQl + "where S.BthId = B.BthId and s.StudId = ''" + txt_Studid.Text.ToString() + "''";


SqlCommand command = new SqlCommand(SQ1,con);
try
{
adapter.SelectCommand = command;
adapter.Fill(ds, "OUTPUT");
dt=ds.Tables{"OUTPUT"];
adapter.Dispose();
command.Dispose();
this.GridView1.DataSource = dt;
this.GridView1.DataBind();

}
catch (Exception e1)
{
Label4.Text = e1.Message.ToString();
return;
}




try it and reply


这篇关于使用csharp将数据库中的数据显示到gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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