插入客户记录,然后插入多个关联的记录,不起作用

时间:2014-01-07 02:16:08

标签: asp.net sql vb.net

我试图想出一个尽可能接近问题的标题。

我们有一家药店,我们正努力让客户有能力在线补充处方药。

我们的假设是,客户可能需要同时补充多个处方。

因此,我们的设计分为两部分。

在页面顶部,用户需要提供三条个人信息(姓氏,电子邮件地址,电话号码)。

然后在页面底部是一个动态生成的文本框。

换句话说,用户输入第一个处方药笔芯,如果有其他处方,用户想要重新填写,则用户点击添加另一个补充按钮以提供另一个处方编号。

用户可以提供最多6个处方补充。

这很好用。

我们遇到的问题是将此信息添加到数据库中。

下面提供的代码将客户的个人信息添加到客户表中,但是将预定信息添加到处方表中并不存储信息。

任何想法我做错了什么? 提前致谢

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

1 个答案:

答案 0 :(得分:0)

代码指向错误的表格。

我在解决了这个问题后得到了它。

Employee_Roster是错误的表。