Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello.

I have created a web application that some one insert his personal information into the database.at the end he wants to see his inserted data. so i decided to make a printpage like below :

FirstName =========== FirstNameResultLabel
LastName =========== LastNameResultLabel
BirthDate =========== BirthDateResultLabel

infact i would like to read data from sqlserver and show them in these labels. so here is my code:
C#
    SqlCommand Command = DataProvider.GenerateCommand("[dbo].[Select_IranianGeneralInfoReport_SP]", CommandType.StoredProcedure);
    Command.Parameters.AddWithValue("@IdCode", GlobalVariables.IdCode);
    try
    {
        Command.Connection.Open();
        GlobalVariables.IdCode = int.Parse(Command.ExecuteScalar().ToString());
        SqlDataReader dr = DataProvider.ExecuteDataReader("[dbo].[Select_IranianGeneralInfoReport_SP]", CommandType.StoredProcedure);
        FamilyResultLabel.Text = dr["LastName"].ToString();
        NameResultLabel.Text = dr["FirstName"].ToString();
        dr.Read();

        dr.Close();
        Command.Connection.Close();
    }
    catch (Exception ex)
    {
        EventLogger.Log(ex.Message, LogType.Error);
    }
}


everything is in the PageLoad.
but when i start to debug , and exception occures .

how can i solve this problem ? or better to say how can i read data from database and show them in those above labels?
Posted
Updated 12-Feb-13 17:50pm
v2
Comments
Mithun P 12-Feb-13 23:55pm    
pls post what is the exception u r getting
mohammad ehsan 13-Feb-13 0:10am    
here is the exception:

Procedure or function 'Select_IranianGeneralInfoReport_SP' expects parameter '@IdCode', which was not supplied.

@IdCode either is a PK in my table and a session among my pages.
PIEBALDconsult 13-Feb-13 0:08am    
"int.Parse(Command.ExecuteScalar().ToString());" Don't do that; if it's an int, just cast it!

You seem to copy the IdCode into the parameter before you set it.

And other questionable things.

Hi,

change these code lines.
C#
SqlCommand Command = DataProvider.GenerateCommand("[dbo].[Select_IranianGeneralInfoReport_SP]", CommandType.StoredProcedure);
            Command.Parameters.AddWithValue("@IdCode", GlobalVariables.IdCode);
try
{
    Command.Connection.Open();
    GlobalVariables.IdCode = int.Parse(Command.ExecuteScalar().ToString());
SqlDataReader dr = DataProvider.ExecuteDataReader("Select_IranianGeneralInfoReport_SP", CommandType.StoredProcedure);
FamilyResultLabel.Text = "";
NameResultLabel.Text = "";
while(dr.Read())
{
FamilyResultLabel.Text = dr["LastName"].ToString();
NameResultLabel.Text = dr["FirstName"].ToString();
}
dr.Close();
Command.Connection.Close(); 
}
catch (Exception ex)
{
    EventLogger.Log(ex.Message, LogType.Error);
}


dr.Read() should be excuted before reading the values. and putting that in while loop will avoid exception on zero rows.
refer this article for more information.
SqlDataReader Class[^]
SqlDataReader Class[^]

hope it helps.
 
Share this answer
 
v2
Comments
mohammad ehsan 13-Feb-13 0:09am    
but this exception occures:

here is the exception:

Procedure or function 'Select_IranianGeneralInfoReport_SP' expects parameter '@IdCode', which was not supplied.

@IdCode either is a PK in my table and a session among my pages.
Karthik Harve 13-Feb-13 0:11am    
i have updated only middle lines. you must keep the other code lines as it is.
Karthik Harve 13-Feb-13 0:14am    
check updated answer.
Hi,

You can use something like below:

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

da.SelectCommand = new SqlCommand(@"SELECT * FROM dbo.MyTable", connString);
da.Fill(ds, "dbo.MyTable");
dt = ds.Tables["dbo.MyTable"];

foreach (DataRow dr in dt.Rows)
{
    Label1.Text=dr["Column1"].ToString();
    Label2.Text=dr["Column1"].ToString();

} 


The above is a method, but it will have performance issues when you have a large table in the database, coz the table will be queried everytime and it will add a overhead to your code.In that case you can pull the whole table in a datatable at once and use it.

Hope it helps.

Regards
Anurag
 
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