(13-10-18, 10:14 PM)محمد خيري كتب : السلام عليكم.معتز بانضمامي لكم اخواني.
هذا الكود يحفظ بقاعدة البيانات بصورة عادية لما بكتب التاريخ بالتاكست 3و لكن لما يكون تاكست التاريخ رقم 3 فارغا ينتج لي خطا
و انا احيانا لازم اخلي التاكست 3 فارغ .كيف اتخطي هذي المشكلة رحم الله والديكم
الكود بالفورم 1كود :
Conversion from string "" to type 'Date' is not valid.
كود :
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
InsertNewRowIn_CASE(TextBox1.Text, TextBox2.Text, TextBox3.Text)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Data_configuration()
End Sub
Public Sub InsertNewRowIn_CASE(ByVal TextBox1 As Int32, ByVal TextBox2 As String, ByVal TextBox3 As DateTime)
Dim Cmd As New SqlCommand
With Cmd
.Connection = Conne_Dept
.CommandType = CommandType.Text
.CommandText = "INSERT INTO Table_Product (Product_id , Product_name, Product_date) VALUES ( @Product_id , @Product_name, @Product_date)"
.Parameters.Clear()
.Parameters.AddWithValue("@Product_id", SqlDbType.Int).Value = TextBox1
.Parameters.AddWithValue("@Product_name", SqlDbType.NVarChar).Value = TextBox2
.Parameters.AddWithValue("@Product_date", SqlDbType.DateTime).Value = TextBox3
End With
Try
If Conne_Dept.State = 1 Then Conne_Dept.Close()
Conne_Dept.Open()
Cmd.ExecuteNonQuery()
Conne_Dept.Close()
Cmd = Nothing
MsgBox("تم الحفظ بنجاح")
Catch ex As Exception
Conne_Dept.Close()
MsgBox(Err.Description, MsgBoxStyle.Information)
Finally
If Conne_Dept.State = ConnectionState.Open Then Conne_Dept.Close()
End Try
End Sub
End Class
جرب هذا
كود :
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
InsertNewRowIn_CASE(TextBox1.Text, TextBox2.Text, TextBox3.Text)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Data_configuration()
End Sub
Public Sub InsertNewRowIn_CASE(ByVal TextBox1 As Int32, ByVal TextBox2 As String, ByVal TextBox3 As String)
Dim sql As String = "INSERT INTO Table_Product (Product_id , Product_name, Product_date) VALUES ( @Product_id , @Product_name, @Product_date)"
Dim Cmd As New SqlCommand(sql, Conne_Dept)
With Cmd
.Parameters.Clear()
.Parameters.AddWithValue("@Product_id", TextBox1)
.Parameters.AddWithValue("@Product_name", TextBox2)
.Parameters.AddWithValue("@Product_date", If(IsDate(TextBox3), CDate(TextBox3), DBNull.Value))
End With
Try
If Conne_Dept.State <> ConnectionState.Open Then Conne_Dept.Open()
Cmd.ExecuteNonQuery()
Cmd = Nothing
MsgBox("تم الحفظ بنجاح")
Catch ex As Exception
Conne_Dept.Close()
MsgBox(Err.Description, MsgBoxStyle.Information)
Finally
Conne_Dept.Close()
End Try
End Sub
End Class(13-10-18, 11:52 PM)محمد خيري كتب : شكرا اخي عاصم على المساعدة الان صار الكود يحفظ صحيح لما بتكون قيمة التاكست 3 فارغة بس لما اكتب التاريخ يطلعلي هذا الايرور
convertion failed when converting date/ or time from character string
جرب هذا
كود :
TextBox3.Text = If(IsDBNull(row.Item("Product_date")), "", row.Item("Product_date").ToString("dd/MM/yyyy"))