Click here to Skip to main content
15,908,274 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
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)



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


Show Button code as follows;


C#
 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;
        }

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.
Posted
Updated 1-May-13 1:22am
v2
Comments
ZurdoDev 1-May-13 8:00am    
The first problem is you are open to SQL injections, meaning I could do anything I want to your database. You need to use parameters in your queries. Second, what line of code is causing the error?

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900