Dazzer1966

Stored Procedure to textBox

May 16th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1.  //!=======================================
  2.         //!GETS ALL RECORDS FROM DATABASE==WORKING
  3.         //!=======================================
  4.         BindingSource binder = new BindingSource();
  5.         private void btnGetAll_Click(object sender, EventArgs e)
  6.         {
  7.             string conString;
  8.             conString = Properties.Settings.Default.jobsConnectionString;
  9.  
  10.             SqlConnection con;
  11.             con = new SqlConnection(conString);
  12.  
  13.             con.Open();
  14.  
  15.             SqlCommand cmd;
  16.             cmd = new SqlCommand("AllRecords", con);
  17.  
  18.             SqlDataAdapter adaptor;
  19.             adaptor = new SqlDataAdapter(cmd);
  20.  
  21.             DataTable dataTable;
  22.             dataTable = new DataTable();
  23.  
  24.             adaptor.Fill(dataTable);
  25.  
  26.             dataGridView1.DataSource = binder;
  27.  
  28.             binder.DataSource = dataTable;
  29.             con.Close();
  30.         }
  31.  
  32.         //!=========================================================
  33.         //!ATTEMPT TO GET STORED PROCEDURE TO DISPLAY INTO A TEXTBOX
  34.         //!=========================================================
  35.         private void button1_Click(object sender, EventArgs e)
  36.         {
  37.  
  38.             string conString;
  39.             conString = Properties.Settings.Default.jobsConnectionString;
  40.  
  41.             SqlConnection con;
  42.             con = new SqlConnection(conString);
  43.  
  44.             con.Open();
  45.  
  46.             SqlCommand cmd;
  47.             cmd = new SqlCommand("SumTest", con);
  48.  
  49.             SqlDataAdapter adaptor;
  50.             adaptor = new SqlDataAdapter(cmd);
  51.  
  52.             SqlDataReader reader;
  53.             reader = cmd.ExecuteReader();
  54.             while (reader.Read())
  55.             {
  56.                 textBox1.Text = reader["SumTest"].ToString();
  57.             }
  58.  
  59.             con.Close();
  60.         }
  61.  
  62. RETURNS ERROR: System.IndexOutOfRangeException: 'SumTest'
Add Comment
Please, Sign In to add comment