Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to show each coloumn with its specified value but my program takes ID coloumn and to show only Id it shows name and adress too.... plz let me know what i have to do



SP is here
SQL
ALTER PROCEDURE [dbo].[GetEmpInfo ]
AS
BEGIN
    select * from EmpData
END




and code is here
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace DataBaseConnectivity2
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conect = new SqlConnection("Data Source=SHARP-ITECH-PC\\SQLEXPRESS;Initial Catalog=EmployeeRecord;Integrated Security=True");
            SqlCommand comand = new SqlCommand("GetEmpInfo", conect);
            comand.CommandType = CommandType.StoredProcedure;
            try
            {
                conect.Open();
                SqlDataAdapter DAdapt = new SqlDataAdapter(comand);
                DataSet dsEmployeeData = new DataSet();
                DAdapt.Fill(dsEmployeeData);
                <pre lang="cs">if (dsEmployeeData != null && dsEmployeeData.Tables.Count > 0)
                {
                    foreach (DataTable dt in dsEmployeeData.Tables)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            Console.Write(dc.ColumnName);
                            {
                                foreach(DataRow dr in dt.Rows)
                                {
                                    foreach (object value in dr.ItemArray)
                                    {
                                        Console.Write(value + " ");
                                    }
                                    Console.WriteLine(" ");
                                }
                            }
                        }
                    }
                }

            }
            finally
            {
                conect.Close();
            }
            Console.ReadKey();
        }
    }
}
Posted
Updated 17-Sep-13 22:44pm
v2

Why do you use stored procedures for a simple selection?

If you use just a select statment it is even easier:
C#
SqlConnection conect = new SqlConnection("Data Source=SHARP-ITECH-PC\\SQLEXPRESS;Initial Catalog=EmployeeRecord;Integrated Security=True");

SqlCommand comand = new SqlCommand("select * from EmpData", conect);

try
{
    conect.Open();

    using (SqlDataReader reader = comand.ExecuteReader())
    {
        while (reader.Read())
        {
           for (int i = 0; i < reader.FieldCount; i++)
	   {
                Console.Write(reader.GetName(i) + ": ");
                Console.WriteLine(reader.GetValue(i));
	   }

   	   Console.WriteLine();
        }
    }
}

finally
{
    conect.Close();
}

Console.ReadKey();
 
Share this answer
 
v3
i have just end the foreach colomn loop to get all coloumns



C#
if (dsEmployeeData != null && dsEmployeeData.Tables.Count > 0)
                {
                    foreach (DataTable dt in dsEmployeeData.Tables)
                    {
                        foreach (DataColumn dc in dt.Columns)// just enclose loop
                        {
                            Console.Write(dc.ColumnName + " ");
                        }

                        foreach (DataRow dr in dt.Rows)
                        {
                            foreach (object value in dr.ItemArray)
                            {
                                Console.Write(value + " ");
                            }
                            Console.WriteLine(" ");
                        }


                    }
                }
 
Share this answer
 
v2
Comments
Innocent910 18-Sep-13 6:03am    
but ther is a problem now i am geting output like this
Id name addrees 1 khawar khawaraddress
but i want this type of output
Id name addrees
1 khawar khawaraddress
how can i get this??????
Pheonyx 18-Sep-13 6:08am    
Add the results of the each for each to a stringbuilder
then Use Console.WriteLine instead of Console.Write

So the first for loop looks like this:

StringBuilder sb = new StringBuilder();
foreach (DataColumn dc in dt.Columns)
{
sb.Append(dc.ColumnName + " ");
}
Console.WriteLine(sb.ToString().Trim());

Then the second loop should look like this:

foreach(DataRow dr in dt.Rows)
{
sb = new StringBuilder();
foreach(var value in dr.ItemArray)
{
sb.Append(value + " ");
}
Console.WriteLine(sb.ToString().Trim());
}
Innocent910 18-Sep-13 6:18am    
got the right answer thnx alot
Innocent910 19-Sep-13 4:18am    
can u plz show me how to use appendformat....
Innocent910 19-Sep-13 4:19am    
can u plz show me how to use appendformat....i want specific coloumn lenght

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