بسم الله الرحمن الرحيم
اخوانى عندى مشكلة فى كود البحث فى قواعد بيانات sql
حيث عندى البحث بالاسم يطلع لى خطأ وهو ساعدونى
ExecuteReader: Connection property has not been initialized
sqlstatment = " "
sql1 = " "
Dim firstcont As Boolean = True
If txtquery1.Text <> " " Then
If firstcont = False Then
sqlstatment = sqlstatment & " patient . id like '%" & txtquery1.Text & "%'"
Else
firstcont = False
sqlstatment = sqlstatment & " patient . id like '%" & txtquery1.Text & "%'"
End If
End If
'sqlcon.Open()
sql1 = " Select * from patient where " & sqlstatment
Dim cmd As New SqlCommand(sql1)
'cmd.Connection = sqlcon
Dim dr As SqlDataReader = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
'txtrec1.Text = dr.Item(" id ")
txtname.Text = dr.Item("name")
txtid.Text = dr.Item("id")
cbojob.SelectedValue = dr.Item("jobid")
txtdate.MyText = dr.Item(" date")
txttel.Text = dr.Item("tel ")
txtphone.Text = dr.Item(" phone")
txtaddress.Text = dr.Item(" address")
txtcomment.Text = dr.Item("comments")
sqlcon.Close()
Else
lblerror.BackColor = Color.Red
lblerror.BackColor = Color.White
lblerror.Text = "السجل غير موجود "
If sqlstatment = ConnectionState.Open Then
sqlcon.Close()
End If
End If
End Sub
اخي اكتب الكود بالتعليق بداخل مربع php مثل هذا
ثانيا : تظهر لك رسالة خطاء اثناء التنفيذ انقل لنا رسالة الخطاء مرفق معها الكود
-
السبب انك لم تفتح الإتصال وبالنظر للكود وجدت انك معطل السطر التالي بعلامة '
[quote='عابر سبيل' pid='13507' dateline='1399198800']
اخي اكتب الكود بالتعليق بداخل مربع php مثل هذا
[Imports System.Data.SqlClient
Imports System.Globalization
Public Class ser
Public Shared sqlcon As New SqlConnection
Dim sqlstatment As String
Dim sql1 As String
Dim winclass As New Class1
Private Sub ser_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
''winclass.Fillcombobox(cbojob, " jobs", "jobname", "jobid", " ")
'cbojob.SelectedIndex = -1
End Sub
Private Sub btnquery1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnquery1.Click
'--------------------------------------------------------
If sqlcon.State = ConnectionState.Open Then
sqlcon.Close()
End If
btnquery1.Enabled = False
btnnewquery1.Enabled = True
'If txtquery1.Text.Trim = " " Then
'MsgBox(" ادخل رقم الهوية ", MsgBoxStyle.Information)
'txtquery1.Focus()
'lblerror.TabIndex = "ادخل رقم الهوية لاجراء البحث "
' lblerror.ForeColor = Color.Red
'Exit Sub
'sqlcon.Close()
'End If
'If sqlcon.State = ConnectionState.Open Then
'sqlcon.Close()
'End If
sqlstatment = " "
sql1 = " "
Dim firstcont As Boolean = True
If txtquery1.Text <> " " Then
If firstcont = False Then
sqlstatment = sqlstatment & " patient . id like '%" & txtquery1.Text & "%'"
Else
firstcont = False
sqlstatment = sqlstatment & " patient . id like '%" & txtquery1.Text & "%'"
End If
End If
'sqlcon.Open()
sql1 = " Select * from patient where " & sqlstatment
Dim cmd As New SqlCommand(sql1)
cmd.Connection = sqlcon
Dim dr As SqlDataReader = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
'sqlcon.Open()
'txtrec1.Text = dr.Item(" id ")
txtname.Text = dr.Item("name")
txtid.Text = dr.Item("id")
cbojob.SelectedValue = dr.Item("jobid")
txtdate.MyText = dr.Item(" date")
txttel.Text = dr.Item("tel ")
txtphone.Text = dr.Item(" phone")
txtaddress.Text = dr.Item(" address")
txtcomment.Text = dr.Item("comments")
sqlcon.Close()
Else
lblerror.BackColor = Color.Red
lblerror.BackColor = Color.White
lblerror.Text = "السجل غير موجود "
If sqlstatment = ConnectionState.Open Then
sqlcon.Close()
End If
End If
End Sub]
[رسالة الخطأ تقول The ConnectionString property has not been initialized]
-
استبدل الكود بما يلي وجرب....
(مراجعة ثانية)
كود :
Dim sql As String = " "
Dim where As String = " "
Dim firstcont As Boolean = True
If txtquery1.Text.Trim() <> "" Then
If firstcont = False Then
where &= " WHERE [id] LIKE '%" & txtquery1.Text.Trim() & "%'"
Else
firstcont = False
where &= " WHERE [id] LIKE '%" & txtquery1.Text.Trim() & "%'"
End If
End If
sql = " SELECT * FROM [patient] " & where
Dim da As New SqlDataAdapter(sql , sqlcon)
Dim dt As New DataTable
If da.Fill(dt) > 0 Then
Dim row As DataRow = dt.Rows(0)
'txtrec1.Text = row.Item(" id ")
txtname.Text = row.Item("name")
txtid.Text = row.Item("id")
cbojob.SelectedValue = row.Item("jobid")
txtdate.MyText = row.Item(" date")
txttel.Text = row.Item("tel ")
txtphone.Text = row.Item(" phone")
txtaddress.Text = row.Item(" address")
txtcomment.Text = row.Item("comments")
Else
lblerror.BackColor = Color.Red
lblerror.BackColor = Color.White
lblerror.Text = "السجل غير موجود "
End If
جربت الكود لكن يطلع لى نفس الخطأ
The ConnectionString property has not been initialized
اما هذه الجملة If da.Fill(dt) > 0 Then
-
راجع الكود السابق
(بعد التعديل) مع التأكد من جملة الإتصال
ConnectionString هل هي صحيحة.
كود :
Dim sqlcon As New SqlConnection(".......")
شكرا لك اخى لقد حلت المشكلة اسأل الله ان تكون فى ميزان حسناتك يا رب
لدى مشكلة اخرى معلبش اخى
عندى كود عمل تعديلات على البيانات
ولكن تطلع لى خطأ فى تعديل البيانات
هاهو الكود
[ Private Sub btnsaveupdate1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsaveupdate1.Click
Dim updateok As Boolean = winclass.SQLExciute(" update patient set name= " & txtname.Text & "' ,id= '" & txtid.Text & "',comments='" & txtcomment.Text & "' where id= " & txtrec1.Text) '")
If updateok = True Then
lblerror.Text = (" تمت عملية تعديل البيانات ")
lblerror.BackColor = Color.Green
lblerror.ForeColor = Color.White
btnsaveupdate1.Enabled = True
Else
MsgBox("خطأ فى عملية تعديل البيانات ", MsgBoxStyle.Critical)
End If
End Sub]