나는 데이터베이스에서 날짜로 모든 날짜 값을 가져 왔지만 valye 09/11/2014..its를 수락 할 때 받아 들일 수 있지만 -30/10/2014..its의 값을 부여하면 이런 종류의 오류가 발생합니다. 왜. 도움이 필요합니다 ...varchar 데이터 형식을 datetime 데이터 형식으로 변환하면 범위를 벗어나는 예외가 발생합니다 ..?
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'TODO:add data into data base
If ComboBox1.Text <> "" Then
Dim ab As Long
ab = random.Next("10000", "99999")
Dim date1 As Date = Date.ParseExact(TextBox3.Text, "dd/MM/yyyy", Nothing).Date
Dim date2 As Date = Date.ParseExact(TextBox4.Text, "dd/MM/yyyy", Nothing).Date
Dim date3 As Date = Date.ParseExact(TextBox5.Text, "dd/MM/yyyy", Nothing).Date
'Dim date3 As Date = Date.Parse(TextBox5.Text)
Dim date4 As Date = Date.ParseExact(TextBox6.Text, "dd/MM/yyyy", Nothing).Date
If Button1.Enabled = False Then
If TextBox11.Text <> "" And ComboBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "" And TextBox4.Text <> "" Then
TextBox10.Text = ab
cm.Close()
cm.Open()
ComboBox1.Text = Replace(ComboBox1.Text, "'", "''")
TextBox1.Text = Replace(TextBox1.Text, "'", "''")
TextBox7.Text = Replace(TextBox7.Text, "'", "''")
TextBox6.Text = Now.Date.ToString("dd/MM/yyyy")
TextBox8.Text = DateTime.Now.ToString("HH:mm:ss")
cmd.CommandText = "insert into worker(faccno,fname,faddr,famcdue,fjoindate,fdate,fattended,fproblem,fsolution,fstatus,fremark,fassign,findate,fintime,fserviceno,falert)values ('" & TextBox11.Text & "', '" & ComboBox1.Text & "','" & TextBox2.Text & "','" & date1.ToString("dd/MM/yyyy") & "','" & date2.ToString("dd/MM/yyyy") & "','" & date3.ToString("dd/MM/yyyy") & "','" & ComboBox4.Text & "','" & TextBox1.Text & "','" & TextBox7.Text & "','" & ComboBox2.Text & "','" & ComboBox6.Text & "','" & ComboBox5.Text & "','" & date4 & "','" & TextBox8.Text & "','" & TextBox10.Text & "','" & ComboBox3.SelectedIndex & "')"
cmd.ExecuteNonQuery()
MessageBox.Show("Information Insertion sucessfull", "Save")
MessageBox.Show(ab, "your service no is")
Button7.PerformClick()
cm.Close()
Else
MessageBox.Show("enter all values then try to save information", "error")
Exit Sub
End If
ElseIf ComboBox1.Text <> "" Then
cm.Close()
cm.Open()
TextBox1.Text = Replace(TextBox1.Text, "'", "''")
TextBox7.Text = Replace(TextBox7.Text, "'", "''")
TextBox6.Text = Now.Date.ToString("dd/MM/yyyy")
TextBox8.Text = DateTime.Now.ToString("HH:mm:ss")
ComboBox4.Text = Replace(ComboBox4.Text, "'", "''") 'TODO:this code replaces single quotes to store in data base
cmd.CommandText = "update worker set faccno= '" & TextBox11.Text & "', fname='" & ComboBox1.Text & "',faddr='" & TextBox2.Text & "',famcdue='" & date1 & "',fjoindate='" & date2 & "',fdate='" & date3 & "',fattended='" & ComboBox4.Text & "',fproblem='" & TextBox1.Text & "',fsolution= '" & TextBox7.Text & "',fstatus='" & ComboBox2.Text & "',fremark='" & ComboBox6.Text & "',fassign= '" & ComboBox5.Text & "',findate='" & date4 & "',fintime='" & TextBox8.Text & "',fserviceno='" & TextBox10.Text & "',falert='" & ComboBox3.SelectedIndex & "' where fserviceno='" & TextBox10.Text & "'"
cmd.ExecuteNonQuery()
MessageBox.Show(" Information Updation sucessfull", "Save")
Button7.PerformClick()
cm.Close()
End If
Else
MessageBox.Show("enter all values then try to save information", "error")
Exit Sub
End If
End Sub
당신의 코드는 당신이'Replace'를 사용 함에도 불구하고 SQL injection 공격에 취약합니다. 또한 ToString이나 Parse에 대한 명시적인 문화권을 지정하지 않았을 때 누군가 다른 날짜/시간 설정을 사용하면 실패합니다. – Dai