Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table named "Workers" created in Microsoft SQL Server 2005. It has 4 Columns - ID, FirstName, LastName, and JobTitle.... They are already filled with some values like 1, Vicky, Sriv, QA Lead. Then i wrote a very simple stored procedure of select * from Workers and named it as "usp_Displayallusers".
Till the above said steps everything worked fine for me..... I can run the same procedure using "exec usp_displayallusers"

But the real problem i got is calling it in Microsoft Visual Studio 2008 (C# Windows Project/Solution). I know it can be called via DataTable, DataSets or DataGrids. I read very good articles here that really made me aware about them. But still i was not able to solve this problem, so please tell me complete code, so that my dumb brain might get something in it, lol :)
Posted
Updated 24-Aug-11 19:05pm
v3

 
Share this answer
 
Comments
walterhevedeich 25-Aug-11 2:03am    
I don't see why this deserve a 1 either. Voted 5 to counter.
HarinderS 25-Aug-11 2:07am    
I gave it 1 star, because he tried, to help me :)
I like people trying to help others :)

The vote by me is not to demoralize om prakash, but it is just a praise even if i didn't got what i wanted.
The example is for ConsoleApplication though i clearly wrote that i am working in Windows Application... and moreover i had to visit the link, my net is slow so it wasted lot of time. Sorry OM, no offense to ya, hope you didn't mind.
I guess you want to display all records in a GridView, if yes then try this:

C#
 string strcon = "Data Source=ABC-PC\\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True";
        SqlConnection conn;

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(strcon);
                conn.Open();
                SqlCommand cmd = new SqlCommand("usp_Displayallusers", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
                DataSet ds = new DataSet();


C#
da.Fill(ds);
                this.GridView1.DataSource = ds;
                this.GridView1.DataBind();

            }
            catch(Exception ex)
            {
                Response.Clear();
                Response.Write("<h4>" + ex.Message.ToString() + "</h4>");
            }
        }



hope it helps :)

for further queries comment here!!
 
Share this answer
 
Comments
HarinderS 25-Aug-11 1:57am    
Thanks, though i didn't wanted to show it in DataGrid, but still i got your point and i implemented it my way. Let me show you what i really wanted..... Check my post below... (Self solution)
Uday P.Singh 25-Aug-11 2:01am    
Glad that it make you clear, though the answer was of asp.net C# , but it shows how can you call stored procedures.
HarinderS 25-Aug-11 2:02am    
Yes, it helped :)

I am working in Visual Studio 2008 C# Windows Applications though... HE HE...
Uday P.Singh 25-Aug-11 2:18am    
yeah I knew it, as i mentioned it in my previous comment, but calling the stored procedure is same in both.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CallingStoredProcedures
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable table = new DataTable();
        int i=0;
        int MaxRows; 
        private void btnGet_Click(object sender, EventArgs e)
        {

            if (i < MaxRows)
            {
                textBox1.Text = table.Rows[i][0].ToString();
                textBox2.Text = table.Rows[i][1].ToString();
                textBox3.Text = table.Rows[i][2].ToString();
                i++;
            }           
     }
        public DataTable GetTable()
        {            
            table.Columns.Add("FirstName", typeof(string));
            table.Columns.Add("LastName", typeof(string));
            table.Columns.Add("JobTitle", typeof(string));
            return table;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetTable();
            SqlConnection con = new SqlConnection("Persist Security Info=False;database=TestDB;server=xyz;Connect Timeout=30;user=abc;pwd=****;");
            con.Open();
            SqlCommand command = new SqlCommand("usp_displayallusers", con);
            command.CommandType = CommandType.StoredProcedure;
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                table.Rows.Add(reader[1].ToString(), reader[2].ToString(), reader[3].ToString());
            }
            MaxRows = table.Rows.Count;
        }
   }
}



The result i am getting is navigation of my records that are already in my SQL Database table "Workers".
On every click, it shows next row on my Visual Form.
 
Share this answer
 
v4
Comments
Uday P.Singh 25-Aug-11 2:03am    
Exactly :) keep learning!!
HarinderS 25-Aug-11 2:06am    
Thanks :)
Not sure what your real problem here is, since you mentioned that you know it can be retrieved and put to DataTable, DataSets, DataGrids, etc. But anyway, to retrieve data from the database, you need to be familiar on the following objects(There are other objects that you need to learn, but just by learning these classes, you can already retrieve from your database).

SqlConnection[^] - This is your connection to your data source, obviously.
SqlCommand[^] - This is where you will set the SQL statement that you are going to execute on the database. Since you are using stored procedures, putting the name of the SP on the CommandText property of the command does the trick. For cases where your SP has parameters, you will need to add parameters on the Parameters property of the command.
DataAdapter[^] - This is the one that you need to fill your data source(DataTable,DataSet).

For some examples, check out the following articles
http://msdn.microsoft.com/en-us/library/dw70f090%28v=VS.90%29.aspx[^]
Using ADO.NET for beginners[^]
 
Share this answer
 
Comments
walterhevedeich 25-Aug-11 2:02am    
To the one who downvoted this, mind to explain why it deserved a 1?
HarinderS 25-Aug-11 2:08am    
simply because you tried to help me out at least.... doesn't matter if i got what i wanted or not....
It showed the kind heart of yours...
Don't take it as a downvote -- it was simply because i already knew what you told.

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