sa san

sa san

  • NA
  • 23
  • 0

how to read values using datareader

Jan 16 2008 1:59 AM

here is example: datareader needs to closed first, already datareader opened try to close first one is usual error ocuured.

public void getAll(string accstr,string acc)

{

if (accstr =="Phone")

{

SqlConnection con = new SqlConnection(cn);

con.Open();

SqlCommand cmd = new SqlCommand(" select JanPhone,FebPhone,MarPhone,AprPhone,MayPhone,JunPhone,JulyPhone,AugPhone,SepPhone,OctPhone,NovPhone,DecPhone from Acc_MVP_Table1 where AccSlno='" + acc + "'", con);

SqlDataReader dr;

dr = cmd.ExecuteReader();

if (dr.Read())

{

//int m1,m2,m3,m5,m4;

//if(dr.GetInt32(0).Equals(1))

if (dr.GetString(0).Equals("3"))

{

dr.Close();

SqlCommand cmd1 = new SqlCommand("select JanComments from Acc_MVP_Table1 ", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr1.Read())

{

string s1 = dr1.GetString(0);

}

dr1.Close();

}

if (dr.GetString(1).Equals("3"))

{

dr.Close();

SqlCommand cmd1 = new SqlCommand("select FebComments from Acc_MVP_Table1", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr1.Read())

{

string s2 = dr1.GetString(0);

}

 

}

if (dr.GetString(2).Equals("3"))

{

dr.Close();

SqlCommand cmd1 = new SqlCommand("select MarComments from Acc_MVP_Table1", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr1.Read())

{

string s3 = dr1.GetString(0);

}

dr1.Close();

}

if (dr.GetString(3).Equals("3"))

{

dr.Close();

SqlCommand cmd1 = new SqlCommand("select AprComments", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr.Read())

{

string s4 = dr1.GetString(0);

}

}

if (dr.GetString(4).Equals("3"))

{

dr.Close();

SqlCommand cmd1 = new SqlCommand("select MayComments", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr.Read())

{

string s5 = dr1.GetString(0);

}

}

if (dr.GetString(5).Equals("3"))

{

SqlCommand cmd1 = new SqlCommand("select JunComments", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr.Read())

{

string s6 = dr1.GetString(0);

}

}

if (dr.GetString(6).Equals("3"))

{

SqlCommand cmd1 = new SqlCommand("select JulyComments", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr.Read())

{

string s7 = dr1.GetString(0);

}

}

if (dr.GetString(7).Equals("3"))

{

SqlCommand cmd1 = new SqlCommand("select AugComments", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr.Read())

{

string s8 = dr1.GetString(0);

}

}

if (dr.GetString(8).Equals("3"))

{

SqlCommand cmd1 = new SqlCommand("select SepComments", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr.Read())

{

string s9 = dr1.GetString(0);

}

}

if (dr.GetString(9).Equals("3"))

{

SqlCommand cmd1 = new SqlCommand("select OctComments", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr.Read())

{

string s10 = dr1.GetString(0);

}

}

if (dr.GetString(10).Equals("3"))

{

SqlCommand cmd1 = new SqlCommand("select NovComments", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr.Read())

{

string s11 = dr1.GetString(0);

}

}

if (dr.GetString(11).Equals("3"))

{

SqlCommand cmd1 = new SqlCommand("select FebComments", con);

SqlDataReader dr1;

dr1 = cmd1.ExecuteReader();

if (dr.Read())

{

string s12 = dr1.GetString(0);

}

}

 

}

con.Close();

}

else if (accstr =="")

{

}

}


Answers (1)