الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ممكن مساعده

مغلق
بدأه Miss-Hearts في 15 أبريل 2007 · 17 رد · 1,530 مشاهدة · في ASP.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

عندي مشكله

لما اربط بين الجداول في مايكورسوفت اكسس تطلع لي

Invalid field defenition ÜserId" in definition of the index or relationship

والظاهر انه في الجدولين الي احاول اربطهم ID

الي هو البريمري كي حاطتهم Autonumber

لما اغير من الاوتونمبر اللى نمبر تستوي العلاقه

سؤالي هل لا يجوز ربط جدولين فيهم البريميري كي اوتونمبر

واذا ممكن تعطوني طريقه كيف اخلى البريمري كي لوحده يزيد عن طريق كود

ومشكله اخرى

لما ادخل البيانات في الـ aspx

ما تتخزن في الداتابيس اذا كانت في علاقات بين الجداول

لما افك العلاقات تتخزن عندي البيانات في الداتابيس

ممكن الحل ..؟.؟؟

#2

وهذا الكود مالي

Dim connection1 As OleDbConnection
		Dim command1 As OleDbCommand
		Dim sql1 As String
		Dim connectionString1 As String


		connectionString1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\HP\My Documents\ZonesCorp\App_Data\ZonesCorp.mdb"
		connection1 = New OleDbConnection(connectionString1)

		Try
			connection1.Open()

			sql1 = "insert into employee_details (emp_name,mother_name,passport_no) values('" & txtApplicantName.Text & "','" & txtMobile.Text & "'," & txtEmail.Text & ")"
			command1 = New OleDbCommand(sql1, connection1)
			Response.Write("The data was insert succesfully")
			command1.ExecuteNonQuery()

		Catch ex As Exception
			Response.Write("Error was accured" & ex.ToString)
			Response.Write(sql1)

			connection1.Close()
		End Try



		Dim connection As OleDbConnection
		Dim command As OleDbCommand
		Dim sql As String
		Dim connectionString As String


		connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\HP\My Documents\ZonesCorp\App_Data\ZonesCorp.mdb"
		connection = New OleDbConnection(connectionString)

		Try
			connection.Open()


			sql = "insert into factory_details (factory_name,telephone,fax) values('" & txtFactoryName.Text & "','" & txtTel.Text & "'," & txtFax.Text & ")"

			command = New OleDbCommand(sql, connection)
			Response.Write("The data was insert succesfully")
			command.ExecuteNonQuery()

		Catch ex As Exception
			Response.Write("Error was accured" & ex.ToString)
			Response.Write(sql)

			connection.Close()
		End Try
#3

أرفق الداتابيز التي فيها الجداول المطلوب ربطها

#4

عندما يكون هنالك علاقه بين قائمتين في ال Database يجب ان تقومي بعمليه الادخال في نفس الوقت ولذالك هنالك حل ان تستعملي

transaction وهي ان الكود لا يتنفذ الا بعد ان يتنفذ كل ال block (اي جملتا الادخال معا في حالتك )

ولكن لا ادري اذا كنت تستطيعين في الاكسس تنفيذ جملة ادخال للجدولين / قائمتين معا: مثلا:

sql="INSERT INTO Main Values"
 sql=sql & "('" & Request.Form("Username") & "',"
 sql=sql & "'" & Request.Form("Password") & "',"
 sql=sql & "'Basic',"
 sql=sql & "'" & Request.Form("Firstname") & "',"
 sql=sql & "'" & Request.Form("Lastname") & "',"
 sql=sql & "'" & Request.Form("Country") & "',"
 sql=sql & "'" & Request.Form("Email") & "')"
 sql=sql & ";"
 sql=sql & "INSERT INTO View Values"
 sql=sql & "('" & Request.Form("Username") & "',"
 ..
 ..
sql=sql & "'')"

اسالي في قسم الاكسس او استعملي ال transaction في ال asp.net

#5

استعملي OleDbTransaction بدل من SqlTransaction في هذا المثال.... وكذالك OleDbCommand

Working With Transactions - the SqlTransaction Class
If you are using Microsoft SQL Server you can use the System.Data.SqlClient.SqlTransaction class to start a transaction. Begin by creating a connection to your database via the SqlConnection class. Next, create a SqlTransaction instance by calling the BeginTransaction() method of the SqlConnection class like so: 

'Create a connection
Dim myConnection As New SqlConnection(myConnString)
myConnection.Open()

'Start the transaction	
Dim myTrans As SqlTransaction = myConnection.BeginTransaction()




Next, create the SqlCommand object that you'll issue your SQL statements through. When creating this object you need to specify that it should use the myTrans SqlTransaction object. You can assign this through the constructor or the SqlCommand's Transaction property. 

... Continued from above ...

Try
   'Specify the first statement to run...
   Dim sql as String = "INSERT INTO ..."

   'Create the SqlCommand object, specifying the transaction through
   'the constructor (along with the SQL string and SqlConnection)
   'Alternatively, could set properties of myCommand
   'to specify the Connection, CommandText, and Transaction...
   Dim myCommand as New SqlCommand(sql, myConnection, myTrans)




Note that we have the SqlCommand object and all command issued to the database within a Try ... Catch block. At this point you can go ahead and issue all of the related commands to the database: 

... Continued from above ...

   myCommand.ExecuteNonQuery()

   'Issue another INSERT
   myCommand.CommandText = "INSERT INTO ..."
   myCommand.ExecuteNonQuery()

   ... Lather, rinse, repeat as needed! ...

   'If we reach here, all command succeeded, so commit the transaction
   myTrans.Commit

Catch ex as Exception
   'Something went wrong, so rollback the transaction
   myTrans.Rollback()

   Throw 		'Bubble up the exception
Finally
   myConnection.Close()	'Finally, close the connection
End Try

تم تعديل هذه المشاركة بواسطة firass88 في 16 أبريل 2007 في 11:44

#6

مشكور اخوي phqs

ان شااء الله راح ارفق لك الداتابيس

#7

يزاك الله خير اخوي فراس

ان شاء الله راح اجرب الطريقه واخبرك شو صار

تسلم اخوي وما قصرت

#8

ممكن اي حد يشوف لي الكود

المشكله بسبب وجود العلاقه بين الجداول

ويظهر لي خطأ

You cannot add or change a record because a related record is required in table 'factory_details'

اعتقد بسبب البريمري كي

لانه هو الوحيد الي مطلوب

علما انه عند فك العلاقات الكود يشتغل وتدخل البيانات في الجدولين مع

البريمري كي تلقائيا بدون ما ادخله

وهذا الكود السمتخدم

Dim connection1 As OleDbConnection

Dim myCommand As OleDbCommand

Dim sql1 As String

Dim connectionString1 As String

Dim myTrans As OleDbTransaction



connectionString1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\HP\My Documents\ZonesCorp\App_Data\ZonesCorp.mdb"

connection1 = New OleDbConnection(connectionString1)



Try

connection1.Open()

myTrans = connection1.BeginTransaction()

sql1 = "insert into employee_details (emp_name,mother_name,passport_no) values('" & txtApplicantName.Text & "','" & txtMobile.Text & "'," & txtEmail.Text & ")"

myCommand = New OleDbCommand(sql1, connection1, myTrans)

myCommand.ExecuteNonQuery()

'Issue another INSERT

myCommand.CommandText = "insert into factory_details (factory_name,telephone,fax) values('" & txtFactoryName.Text & "','" & txtTel.Text & "'," & txtFax.Text & ")"

Response.Write("The data was insert succesfully")

myCommand.ExecuteNonQuery()

myTrans.Commit()

MsgBox(" OK ", MsgBoxStyle.Information)



Catch ex As Exception

Response.Write("Error was accured" & ex.ToString)

Response.Write(sql1)

myTrans.Rollback()

Throw

' Finally

myCommand.Dispose()

connection1.Close()

End Try
#9

brb

#10

السلام عليكم.

في البدايه لقد فهمت اين الخطا وقمت بتغييرات في ال database ولكن اريد قبل كل شيء ان انصحك بهذه النصائح

اولا اختي انصحك بتسمية ال textboxes باسماء مناسبه للحقول

مثلا: في ال sql

myCommand.CommandText = "insert into employee_details (emp_name,mother_name,passport_no,factory_id) values('" & txtApplicantName.Text & "','" & txtMobile.Text & "'," & txtEmail.Text & "," & fact_id & ") "

_passport_no ياخذ قيمه من txtEmail ...

ثانيا انصحك بان تستعملي breakpoints و f10

Run step by step وذالك للكشف عن الاخطاء

اما عن ال database فقد قمت بازاله العلاقات بين factory_details و employ_details وباقي ال tables ومحوت بعض الحقول التي هي index ثم قمت بتغيير الحقل factory_id من autonumber الى number ثم بنيت علاقه 1 ال n وقد اشتغل الكود بعد اجراء بعض التعديلات عليه ايضا.

ارجو بعد ان تفهمي جميع التغييرات ان ترجعي تلك الحقول وباقي العلاقات ولكن يجب ان تاخذي بعين الاعتبار انه يجب ادخال نفس القيمه الى جدولين اذا كان بينهم علاقه ابتداء من ال الجدول الي به العلاقه 1 ثم الاخر (n)

ZoneCorp_new.rar

App_Data.rar

#11

انتبهي اني استعملت ال ZonesCorp2.mdb

وهذا شرح لما اجريته من تغييرات في الكود

	   Dim connection1 As OleDbConnection
		Dim myCommand As OleDbCommand
		Dim sql1 As String
		Dim connectionString1 As String
		Dim myTrans As OleDbTransaction
		Dim fact_id As Long

		connectionString1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\App_Data\ZonesCorp2.mdb"
		connection1 = New OleDbConnection(connectionString1)


		Try

			connection1.Open()
			myTrans = connection1.BeginTransaction()
' اقوم بمعرفة الرقم الاخير عبر استعمال الداله max 

			sql1 = "SELECT max(factory_id) from factory_details"

			myCommand = New OleDbCommand(sql1, connection1, myTrans)
' Executescalar يستعمل عند ارجاع قيمه واحد بعد تنفيذ ال sql
			fact_id = myCommand.ExecuteScalar()
' ثم اقوم بزيادته بواحد
			fact_id += 1

			'Issue another INSERT
' factory_id ثم اقوم بادخال ال 
' للجدولين لان يوجد بينهم العلاقه ابتداء من ال 
' primary key (1)			
			myCommand.CommandText = "insert into factory_details (factory_id,factory_name,telephone,fax) values(" & fact_id & ",'" & txtFactoryName.Text & "','" & txtTel.Text & "'," & txtFax.Text & ");"

			myCommand.ExecuteNonQuery()

			myCommand.CommandText = "insert into employee_details (emp_name,mother_name,passport_no,factory_id) values('" & txtApplicantName.Text & "','" & txtMobile.Text & "'," & txtEmail.Text & "," & fact_id & ") "			

			myCommand.ExecuteNonQuery()
			myTrans.Commit()			
			Response.Write("The data was insert succesfully")


		Catch ex As Exception
			Response.Write("Error was accured" & ex.ToString)
			'Response.Write(sql1)
			myTrans.Rollback()
			Throw
		Finally
			'   Finally
			myCommand.Dispose()
			connection1.Close()

		End Try

ملاحظه:

ال factory_id جعلته من نوع number ولكني لاحظت انك كتبتي ملاحظه انك تريدين ان يكون :

Unique five-character code based on factory name.

ما قصدك ارجو التوضيح لان اذا غيرته الى text عندها ال max لا يعمل

ارجو ان توضحي كيف مثلا اذا كان اسم المصنع pepsi ماذا تودين ان يكون ال primary key" factory_id" ؟

عندها يمكن تعديل الكود....

تم تعديل هذه المشاركة بواسطة firass88 في 27 أبريل 2007 في 19:04

#12

مشكور اخوي فراس يزاك الله خير

فيك الخير ياخوي والله ما تقصر

بس عندي سؤال في الداتابيس شو غيرت ..؟؟..

بس حذفت الحقول وغيرت factory_id

to autonumber

???

لاني حاولت اسوي نفس الشي مع الداتابيس الي عندي الاصليه غيرت من اوتونمبر الى نمبر وعقب رديت العلاقات بين الجداول

وطلعت لي هذي المسج يوم شغلت البرنامج

Conversion from type 'DBNull' to type 'Long' is not valid.

ع العموم مشكور ان شاء الله الحين راح اعدل على الداتابيس وارد الحقول الي حذفتها

بالنسبه لنصيحتك بتسميه الحقول اعرف

بس انا بس جي كنت اجرب انه اذا تتخزن المعلومات والا لا

وعقب بعدل كل التسميات للحقول المناسبه

مشكور اخوي جداااااا

ان شاء الله في ميزان حسناتك

#13

نسيت اخوي اقولك بالنسبه لسؤالك عن factory_id

كنت اريده ياخذ اول خمس احرف من اسم المصنع

#14

في البدايه اود ان اقول ان وضع ال employ_details وال factory_details في نفس الصفحه غير ممكن لان الجدولين مربوطان بعلاقه

1-n بواسطة ال factory_id . ولذالك ولكي تستطيعي ادخال عمال في نفس المصنع يجب الفصل بين الصفحات اي ان تخصصي صفحه لادخال بيانات المصنع ثم صفحه اخرى لادخال بيانات العامل وعندها يمكنك استعمال dropdownlist لعرض جميع اسماء المصانع المتوفره ويمكن للعامل ان ينتسب للمصنع الذي يعمل به - بالطبع يجب اضافة المصنع في البدايه.

وعلى فكره استطعت ادخال البيانات بالصوره الموجوده حاليا دون الحاجه الى transaction ولكن استعمالها افضل اذا استدعى ذالك اي اذا اردت ادخال قيمه في نفس الوقت الى اكثر من جدول وفقط في نفس الوقت.

بالنسبه لتغيير ال factory_id الى text فيمكنك ذالك فقط اذا افرغت المعلومات الموجوده في الجدول واحيانا الروابط ايضا

وذالك يفسر هذه الملاحظه ايضا Conversion from type 'DBNull' to type 'Long' is not valid

وبعد هذا التغيير لا يمكنك استعمال sql1 = "SELECT max(factory_id) from factory_details"

بل يجب اخذ القيمه الموجوده في ال textBox وادخالها في الجدول المطلوب وذالك اسهل

بالتوفيق .... واذا احتجت لمساعده انا في االخدمه....

#15

اها فهمت الحين

صح اليوم جربت احط معلومات في الجدول اول وعقب جربت ادخل البيانات

وما ظهرت لي المسج

Conversion from type 'DBNull' to type 'Long' is not valid

لانه لازم الجدول على الاقل يكون فيه ريكورد واحد

مشكور اخوي ع شرحك والله استفدت اشياء

يزاك الله خير

وان شاء الله اذا احتجت شي راح اقولك ادريبك راعي واجب وما تقصر

#16

السلام عليكم

اخوي فراس ظهرت لي مشكله

هي لما ادخل البيانات في جدول المصنع

اكتشفت انه عقب عدد معين ما يرضى يدخل اي ريكورد زياده

مثلا عندي 31 ريكورد لازم ادخلهم في جدول المصنع

وهذا الكود بس يدخل لي 25

اعتقد عشان الكود وايد طويل

كيف ممكن اكتبه بطريقه مختصره ؟؟

اتمنى فهمت قصدي

وهذا الكود الي مب راضي بدخل البيانات

Dim connection1 As OleDbConnection

Dim myCommand As OleDbCommand

Dim sql1 As String

Dim connectionString1 As String

Dim myTrans As OleDbTransaction

Dim fact_id As Long



connectionString1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|ZonesCorp1.mdb"

connection1 = New OleDbConnection(connectionString1)





Try



connection1.Open()

myTrans = connection1.BeginTransaction()



sql1 = "SELECT max(factory_id) from factory_details"



myCommand = New OleDbCommand(sql1, connection1, myTrans)



fact_id = myCommand.ExecuteScalar()



fact_id += 1



myCommand.CommandText = "insert into factory_details (factory_id,factory_name,telephone,fax,mobile,pobox,file_no,licenses_expiry_d
ate,licenses_issue_date,municipality_expiry_date,municipality_issue_date,envi_exp
iry_date,envi_issue_date,cham_expiry_date,cham_issue_date,est_no,labour_available
_no,labour_total_no,labour_on_leave,bans,other,land_lease_expiry_date,land_lease_
issue_date,land_location,land_area,factory_products,product_desc,remarks) values(" & fact_id & ",'" & txtFactoryName.Text & "','" & txtTel.Text & "','" & txtFax.Text & "','" & txtMobile.Text & "','" & txtPobox.Text & "','" & txtFileNo.Text & "','" & txtZonesExDa.Text & "','" & txtZonesIsDa.Text & "','" & txtMunExDa.Text & "','" & txtMunIsDa.Text & "','" & txtEnvExDa.Text & "','" & txtEnvIsDa.Text & "','" & txtChExDa.Text & "','" & txtChIsDa.Text & "','" & txtEsNo.Text & "','" & txtAvNo.Text & "','" & txtTotalNo.Text & "','" & txtOnLeave.Text & "','" & txtBans.Text & "','" & txtOthers.Text & "','" & txtLaExDa.Text & "','" & txtLaIsDa.Text & "','" & txtLocation.Text & "','" & txtArea.Text & "','" & txtProducts.Text & "','" & txtProDes.Text & "'," & txtRemarks.Text & ");"



myCommand.ExecuteNonQuery()



myCommand.CommandText = "insert into labour_request (request_date,applicant_name,pass_no,represented,required_no,admin,secr,drive
r,tech,factory_id) values('" & txtDate.Text & "','" & txtApplicantName.Text & "','" & txtPassNo.Text & "','" & txtRep.Text & "','" & txtReq.Text & "','" & txtAdmin.Text & "','" & txtSecre.Text & "','" & txtDriver.Text & "'," & txtTech.Text & "," & fact_id & ") "



myCommand.ExecuteNonQuery()

myTrans.Commit()

Response.Write("The data was insert succesfully")





Catch ex As Exception

Response.Write("Error was accured" & ex.ToString)

'Response.Write(sql1)

myTrans.Rollback()

Throw

Finally

myCommand.Dispose()

connection1.Close()



End Try

لكن لما احذف اخر 3 الي هم

txtProducts.Text & "','" & txtProDes.Text & "'," & txtRemarks.Text & ");"

عادي البيانات تتخزن

وهذي المسج اي تظهر لي

No value given for one or more required parameters.

#17

لا اعتقد ان المشكله - لها علاقه بعدد البيانات التي ادخلت بل يبدو ان لديك حقول معمول لها index اي انه لا يريد ان يدخل هذه الحقول مرارا

ولكن هذا فقط توقع .... نظرا لضيق الوقت ارجو ان ترفقي الكود كامل مع الداتابيس اذا قمت بتغييرات داخله ايضا. واذا خفت على الكود ضعي عليه password وابعتيها عل الخاص.

#18

مشكور اخوي

طرشت لك رساله

هذا الموضوع مغلق.

مواضيع مشابهة