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

namespace LMS
{
public partial class Member : Form
{
    public Member()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        CreateNew();
    }


    void CreateNew()
    {
        textBox1.Text = "";
        textBox2.Text = "";
        textBox3.Text = "";
        textBox4.Text = "";
        textBox5.Text = "";
        comboBox1.Text = "";
        comboBox2.Text = "";
        comboBox3.Text = "";
        Connection con = new Connection();
        SqlDataAdapter sda = new SqlDataAdapter("Proc_member", con.ActiveCon());
        sda.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataTable dt = new DataTable();
        sda.Fill(dt);
        textBox1.Text = dt.Rows[0][0].ToString();
        textBox2.Focus();
    }

    }

    }

I can't get any value from this procedure. I have used the same code with different procedure names in a different form (and I have saved all of them). It works perfectly but in other forms it keeps generating this error. I'm just a beginner, so please answer in the simplest way possible.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Satyam Singh
  • 1
  • 1
  • 1

2 Answers2

2

Your data table is empty , you need to execute command before

using (SqlCommand command = new SqlCommand(sSql, con))
{
     using (SqlDataReader reader = command.ExecuteReader())
     {
          while (reader.Read())
          {     
               DataTable dtSchema = reader.GetSchemaTable();
               for (int i = 0; i < reader.FieldCount; i++)
               {
                   var fieldVal = reader.GetValue(i).ToString();
               }
          }
     }
}
Edo Zack
  • 21
  • 2
0
private void button1_Click(object sender, EventArgs e)
{
    string[,] name = new string[2, 3] { {"amir","amir1","amir2" }, { " ali","ali2","ali3" } };
    int[,][] grid = new int[2,3][];
    grid[0,0] = new int[4] { 20, 15, 18, 17 };
    grid[0,1] = new int[2] { 14, 19 };
    grid[0,2] = new int[3] { 16, 14, 20 };
    grid[1,0] = new int[5] { 20, 15, 18, 17,16 };
    grid[1,1] = new int[1] { 19 };
    grid[1,2] = new int[4] { 16, 14, 13,15 };
    for (int i = 0; i < 6; i++)
    {
        int sum = 0;
        for (int j = 0; j < grid.Length - 1; j++)
            **sum = sum + grid[i, i][j];**


        listBox1.Items.Add(name[i,i] + " : " + (sum * 1.0 /
            grid[i,i].Length).ToString("##.00"));
    
    }
}
Jonathan Dodds
  • 2,654
  • 1
  • 10
  • 14
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney May 14 '23 at 19:05