1
我試圖想出一個儘可能接近問題的標題。插入客戶記錄,然後插入多個記錄相關,不起作用
我們有一家藥店,我們正試圖讓我們的客戶能夠在線填充他們的處方藥。
我們推測客戶可能有多個他們想要同時補充的處方。
所以,我們的設計分爲兩部分。
在頁面頂部,用戶需要提供三條個人信息(姓,電子郵件地址,電話號碼)。
然後在頁面的底部是一個動態生成的文本框。
換句話說,用戶輸入第一處方再填充,並且如果存在另一個處方用戶想重新填充,用戶點擊添加另一個筆芯按鈕以提供另一處方號碼。
用戶可以提供最多6個處方補充。
這工作正常。
我們遇到的問題是將此信息添加到數據庫中。
下面提供的代碼將客戶的個人信息添加到Customer表中,但將Presicrions信息添加到Prescriptions表中並未存儲信息。
任何想法我做錯了什麼? 在此先感謝
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim s As String
Dim sql As String
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("App_Data\GCP.mdb")
Try
SetRowData()
Dim table As DataTable = TryCast(ViewState("CurrentTable"), DataTable)
If table IsNot Nothing Then
s = "INSERT INTO Customer(Lastname, Email_Address, Phone) Values (?, ?, ?)"
sql = "Select Max(custId) From Customer"
'Response.Write(s)
'Response.End()
Dim con As New OleDbConnection(connStr)
Dim cmd1 As New OleDbCommand(s, con)
cmd1.Parameters.AddWithValue("", txtlName.Text)
cmd1.Parameters.AddWithValue("", txtemail.Text)
cmd1.Parameters.AddWithValue("", txtphone.Text)
con.Open()
cmd1.ExecuteNonQuery()
cmd1.CommandText = sql
ID = cmd1.ExecuteScalar()
For Each row As DataRow In table.Rows
Dim txRefill As String = TryCast(row.ItemArray(1), String)
If txRefill IsNot Nothing Then
Try
s = "INSERT INTO Employee_Roster(refillNumber, custId) VALUES "
s += "('" & txRefill & "', " & ID & ")"
'Response.Write(s)
'Response.End()
'Dim connStr As String = ConfigurationManager.ConnectionStrings("allstringconstrng").ConnectionString
Dim conn As New OleDbConnection(connStr)
Dim cmd As New OleDbCommand(s, conn)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
'Display some feedback to the user to let them know it was processed
lblResult.ForeColor = System.Drawing.Color.Green
lblResult.Text = "Record successfully saved!"
'Clear the form
txRefill = ""
Catch
'If the message failed at some point, let the user know
lblResult.ForeColor = System.Drawing.Color.Red
lblResult.Text = "Your record failed to save, please try again."
End Try
End If
Next
End If
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
它拋出了什麼異常?在catch塊中將異常值分配給lblResult.Text,這樣你就知道拋出了什麼異常。 –
我剛纔解決了它。 它指向錯誤的表格。 謝謝你試圖hel @AdarshShah – Kenny