محمد عنيبة نشر 11 مارس 2022 أرسل تقرير مشاركة نشر 11 مارس 2022 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.' فهل فيه حد يعرف المشكلة 1 اقتباس رابط هذا التعليق شارك على الشبكات الإجتماعية More sharing options...
0 Wael Aljamal نشر 11 مارس 2022 أرسل تقرير مشاركة نشر 11 مارس 2022 في دالة الزر رقم 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(); اقتباس رابط هذا التعليق شارك على الشبكات الإجتماعية More sharing options...
السؤال
محمد عنيبة
هذا نموذج مربوط بقاعدة البيانات sql server لكنني واجهت مشكلة في زر عرض الكتب التي ألفها مؤلف
System.InvalidOperationException: 'ExecuteReader: لم تتم تهيئة الخاصية Connection.'
فهل فيه حد يعرف المشكلة
رابط هذا التعليق
شارك على الشبكات الإجتماعية
1 جواب على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.