Web ページにボタンがあり、追加のコードで少し助けが必要です。TRY、CATCHステートメントが必要だと思いますか?、これは私がこれまでに持っているものです:
ボタンを押すと、ユーザーはストアドプロシージャを介してDBテーブルにデータを追加できるシンプルなWebページがあります。このボタンを押すと、ポップアップ メッセージ ボックスが表示され、データが渡されたことをユーザーに知らせます。次に、ユーザーはこのメッセージ ボックス内の [OK] ボタンを押す必要があります。これにより、サイトのホームページに移動します。これはうまくいきます。
このコードは次のとおりです。
Protected Sub btnAddRawData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRawData.Click
'database conn, this is linked to the web config file .AppSettings
Using dbconnection As New SqlConnection(ConfigurationManager.AppSettings("dbconnection"))
dbconnection.Open()
'command to state the stored procedure and the name of the stored procedure
Using dbcommand As SqlCommand = dbconnection.CreateCommand
With dbcommand
.CommandType = CommandType.StoredProcedure
.CommandText = "RawData_Insert"
'simply execute the query
dbcommand.ExecuteNonQuery()
'Code to make a pop up work. It enables us to use and call the function
'located on the main Rawdata.aspx page.
Dim cs As ClientScriptManager = Page.ClientScript
Dim csname1 As String = "PopupScript"
Dim cstype As Type = Me.GetType()
Dim cstext1 As New StringBuilder()
cstext1.Append("success();")
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString())
'redirect to the main home page
Response.Redirect("~/default.aspx?")
End With
End Using
End Using
End Sub
ユーザーがデータベースに重複したレコードを入力することを望まないので (これは、ユーザーが btnAddRawData_Click があるページに戻ってもう一度押すことで実行できます)、「DupRecords」という名前の UNIQUE INDEX を作成しました。ユーザーがその日にこのデータを複数回コミットするのを防ぎます。
Web ページを実行すると、ブラウザに次のメッセージが表示されます。
一意のインデックス 'DupRecords' を持つオブジェクト 'dbo.GasRawData' に重複するキー行を挿入することはできません。ステートメントは終了されました。
私が考える解決策は、TRY、CATCH ステートメントを btnAddRawData_Click コードに追加することです。私はプログラミングが初めてで、この分野での経験があまりないので、誰かが私を正しい方向に向けてここに置くのを手伝ってくれますか?
よろしくベティ。