اذهب إلى المحتوى
  • 0

خلل في نموذج C# مربوط بقاعدة بيانات sql server عند الضغط على زر System.InvalidOperationException: 'ExecuteReader: لم تتم تهيئة الخاصية Connection.'

محمد عنيبة

السؤال

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 library
{
    public partial class Form4 : Form
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand com = new SqlCommand();
        SqlDataReader dr;
        DataTable dt = new DataTable();
        public Form4()
        {
            conn.ConnectionString = "server=(local);database=library;integrated security=true;";

            try
            {
                conn.Open();
                MessageBox.Show("فُتح");
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int first_space_index = textBox1.Text.IndexOf(" ");
            string first_name=textBox1.Text.Substring(0,first_space_index);
            string last_name = textBox1.Text.Substring(first_space_index +1); 
           
            com.Connection = conn;
            try
            {
                if (!string.IsNullOrEmpty(textBox2.Text))
                {
                    MessageBox.Show("سيدخل رقم القسم تلقائياً");
                    textBox2.Text = null;
                }
                com.CommandText = "insert into authors (AfirstName,ALastName)values('" + first_name + "','" + last_name + "')";
                com.ExecuteNonQuery();
                MessageBox.Show("تم");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            com.Connection = conn;
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                DialogResult resu = MessageBox.Show("هل أنت متأكد من حذف جميع البيانات", "تأكيد", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (resu==DialogResult.OK)
                {
                    com.CommandText = "delete authors ";
                    com.ExecuteNonQuery();
                    MessageBox.Show("تم");
                }
            }
            else
            {
                com.CommandText = "delete authors where AID='" + textBox2.Text + "' ";
                com.ExecuteNonQuery();
                MessageBox.Show("تم");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            com.Connection = conn;
            try
            {
                if (!string.IsNullOrEmpty(textBox1.Text))
                {
                    int first_space_index = textBox1.Text.IndexOf(" ");
                    string first_name = textBox1.Text.Substring(0, first_space_index);
                    string last_name = textBox1.Text.Substring(first_space_index + 1); ;
                    com.CommandText = "update authors set AfirstName= '" + first_name + "',ALastName='"+last_name+"'where DID='" + textBox2.Text + "'";
                    com.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            MessageBox.Show("تم");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            com.Connection = conn;
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                com.CommandText = "select * from authors";
            }
            else
            { com.CommandText = "select * from authors where AID= '" + textBox2.Text + "'"; }
            dr = com.ExecuteReader();
            dt.Load(dr);
            dataGridView1.DataSource = dt;
        }

        private void button6_Click(object sender, EventArgs e)
        {
           
            com.CommandText = "select book.* from book,authors where AID= '" + textBox2.Text + "'";
            dr = com.ExecuteReader();
            dt.Load(dr);
            dataGridView1.DataSource = dt;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1();
            f.Show();
            this.Hide();
        }
    }
}

هذا نموذج مربوط بقاعدة البيانات sql server لكنني واجهت مشكلة في زر عرض الكتب التي ألفها مؤلف 

 private void button6_Click(object sender, EventArgs e)
        {
           
            com.CommandText = "select book.* from book,authors where AID= '" + textBox2.Text + "'";
            dr = com.ExecuteReader();
            dt.Load(dr);
            dataGridView1.DataSource = dt;
        }

System.InvalidOperationException: 'ExecuteReader: لم تتم تهيئة الخاصية Connection.'

فهل فيه حد يعرف المشكلة

 

 

‏‏لقطة الشاشة (999).png

‏‏لقطة الشاشة (1000).png

‏‏لقطة الشاشة (1001).png

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

في دالة الزر رقم 6 لن تتم تهيئة الاتصال، حاول إضافة التعليمة

            com.Connection = conn;

لتصبح

private void button6_Click(object sender, EventArgs e)
{

// هنا
com.Connection = conn;

com.CommandText = "select book.* from book,authors where AID= '" + textBox2.Text + "'";
dr = com.ExecuteReader();

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...