Click here to Skip to main content
14,741,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is mine code..
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class Default5 : System.Web.UI.Page
{
string strconn = ConfigurationManager.ConnectionStrings["touchconnectionstring7"].ConnectionString;

string conny;
SqlCommand cmd;

protected void Page_Load(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection(strconn);
con.Open();
string conny = "select userid, FIRSTNAME, PASSWORD, EMAILID FROM registration WHERE userid= max(userid);
cmd = new SqlCommand(conny, con);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
Label1.Text = dr["FIRSTNAME"].ToString();
dr.Read();
Label2.Text = dr["PASSWORD"].ToString();
dr.Read();
Label3.Text = dr["EMAILID"].ToString();
dr.Read();
dr.Close();
con.Close();

}





}

this code is not working guys help me its urgent.
Posted
Comments
Kiran Wilson 17-Nov-14 5:08am
   
what is the error you are getting?
Member 10874581 17-Nov-14 5:20am
   
data is not dislpaying to my labels
Member 10874581 17-Nov-14 5:22am
   
and this error is also coming...An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Kiran Wilson 17-Nov-14 5:47am
   
Please check this

http://www.sql-server-helper.com/error-messages/msg-147.aspx

This will help you..
Member 10874581 17-Nov-14 6:04am
   
I am trying this code bt also giving error execute reader : connection property string not intitilized
Member 10874581 17-Nov-14 6:05am
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ string constr = System.Configuration.ConfigurationManager.ConnectionStrings["touchConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter adp = new SqlDataAdapter();

SqlCommand cmd = new SqlCommand( " select userid , FIRSTNAME from register where userid = select( Max(userid) from register)");
con.Open();
SqlDataReader dr = cmd.ExecuteReader();

while( dr.Read())
{
Label1.Text = dr["userid"].ToString();
Label2.Text = dr["FIRSTNAME"].ToString();

}

con.Close();
}

}

1 solution

You need to use a while loop or for each loop to fetch the row data;

Change the code as below and try.

while (dr.Read())
            {
                Label1.Text = dr["FIRSTNAME"].ToString();
                Label2.Text = dr["PASSWORD"].ToString();
                Label3.Text = dr["EMAILID"].ToString();
            }



Check below for an msdn sample on using DataReader.

http://msdn.microsoft.com/en-us/library/haa3afyz%28v=vs.110%29.aspx[^]
   
v3
Comments
Member 10874581 17-Nov-14 6:03am
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ string constr = System.Configuration.ConfigurationManager.ConnectionStrings["touchConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter adp = new SqlDataAdapter();

SqlCommand cmd = new SqlCommand( " select userid , FIRSTNAME from register where userid = select( Max(userid) from register)");
con.Open();
SqlDataReader dr = cmd.ExecuteReader();

while( dr.Read())
{
Label1.Text = dr["userid"].ToString();
Label2.Text = dr["FIRSTNAME"].ToString();

}

con.Close();
}

}
giving error connecction execute reader: connection property not initilized what is this?
Mathew Soji 17-Nov-14 6:10am
   
You have not assigned connection for the command object.

con.Open();
cmd.Connection = con; //add this line once connection is opened.
SqlDataReader dr = cmd.ExecuteReader();

while( dr.Read())
{
Label1.Text = dr["userid"].ToString();
Label2.Text = dr["FIRSTNAME"].ToString();

}

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