0

I am totally new to C#. I have created one small table that has 6 records. I want to get those records using a store procedure. Also, I have simple two forms. One is the Main Form that user can type Student Id to get the result by click on the find button.

GetStudentById

CREATE Procedure GetStudentById (@Id VARCHAR)
AS
BEGIN
    SELECT * FROM dbo.Students WHERE Id = @Id
END

I have tried to find a solution my own. Because, I am learning this.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinApp
{
    public partial class WinForm : Form
    {
        public WinForm()
        {
            InitializeComponent();
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }// End CloseBtn_Click

        private string getConnectionString()
        {
            string conStr = ConfigurationManager.ConnectionStrings["WinApp.Properties.Settings.WinPro_dbConnectionString"].ToString();
            return conStr;
        }

        private void FindBtn_Click(object sender, EventArgs e)
        {
            string SearchId = SearchInput.Text;

            SqlCommand cmd = null;
            using (SqlConnection con = new SqlConnection( getConnectionString() ))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.CommandText = "GetStudentById";
                    cmd.Parameters.AddWithValue("@Id", SearchId);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    con.Close();
                    cmd = null;
                }
            }
        }// End FndBtn_Click
    }
}

Above code from my main form. How can I get that result to an array and assign to my second viewform controls to edit the result?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
yeshansachithak
  • 832
  • 2
  • 18
  • 34
  • http://stackoverflow.com/questions/4269548/executenonquery-for-select-sql-statement-returning-no-rows .... http://msdn.microsoft.com/en-us/library/d7125bke.aspx ....you need to use ExecuteReader....then read the rows with SQLDataReader.Read – Colin Smith Aug 10 '14 at 13:53

1 Answers1

1
    SqlCommand cmd = null;
    using (SqlConnection con = new SqlConnection( getConnectionString() ))
    {
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "GetStudentById";
            cmd.Parameters.AddWithValue("@Id", SearchId);

            SQLDataReader reader = cmd.ExecuteReader();

            // To make your code a bit more robust you should get
            // the indexes of the named columns...this is so that if
            // you modified the Schema of your database (e.g. you
            // added a new column in the middle, then it is more
            // resilient than using fixed value index numbers).

            int iId = oReader.GetOrdinal("Id");
            int iFirstname = oReader.GetOrdinal("Firstname");
            int iLastname = oReader.GetOrdinal("Lastname"); 

            while(reader.Read())
            {
                int id = reader.GetInt32(iId);
                string firstname = reader.GetString(iFirstname);
                string lastname = reader.GetString(iLastname);

                ....
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            cmd = null;
        }
    }
Colin Smith
  • 12,375
  • 4
  • 39
  • 47