【问题标题】:Posting data to a SQL Table via vb.net Form with an onClick Submit button通过带有 onClick 提交按钮的 vb.net 表单将数据发布到 SQL 表
【发布时间】:2016-08-24 00:27:49
【问题描述】:

我是 VB.net 的新手,我正在 Visual Studio 2015 中开发一个项目。在我的应用程序选项卡之一中,我试图通过表单帖子将新记录插入 SQL 表,但是一旦我点击该按钮没有任何反应,也没有添加新记录。我当前的代码如下,任何帮助都会很棒!谢谢:

Contact.aspx

<%@ Page Title="Contact" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Contact.aspx.vb" Inherits="Contact" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<head>
<meta charset="utf-8">
</head>
<body>
<br />
<p style="font-size: 15px;">Fill out the form below and click SUBMIT once completed:</p>
<form id="submitData" method="post">
<fieldset>
  <p><label for="Term">Term:</label>
  <input type="text" name="Term" required />*
  </p>

  <p><label for="genre">Definition:</label>
     <input type="text" name="Definition" required />*
  </p>

  <p><label for="year">Long Description:</label>
     <textarea rows="3" type="text" name="LongDescription" required></textarea>*
  </p>
  <p><label for="title">Category:</label>
     <select name="Category" style="width: 312px; height: 31px; font-family: Arial, Helvetica, sans-serif; font-size: 1.2em;">
      <option selected="selected">
      <option value="Branding">Branding</option>
      <option value="Business">Business</option>
      <option value="Business Management">Business Management</option>
      <option value="CRM">CRM</option>
      <option value="Communications">Communications</option>
      <option value="Dental Health">Dental Health</option>
      <option value="Education">Education</option>
      <option value="General">General</option>
      <option value="Governmental">Governmental</option>
      <option value="IT">IT</option>
      <option value="Marketing">Marketing</option>
      <option value="Nutrition">Nutrition</option>
      <option value="State Laws">State Laws</option>
      <option value="Other">Other...</option>
    </select>
  </p>
    <p><label for="title">Date Loaded:</label>
     <input type="text" name="DateLoaded" id="datepicker" required />*
  </p>
    <p><label for="title">Provided By (your name):</label>
     <input type="text" name="ProvidedBy" required />*
  </p>
    <p><label for="title">Department:</label>
     <input type="text" name="Department" required />*
  </p>
    <p><label for="title">Internal Expert or SME for this topic:</label>
     <input type="text" name="SME" />
  </p>

  <p><asp:Button ID="buttonSubmit" runat="server" OnClick="buttonSubmit_Click" Text="Submit" /></p>
</fieldset>

 </form>
<br />
<p>* Please allow between 24 to 48 hours to have your term added to the dictionary.</p>
<br />
<a style="font-size: 16px; font: bold; background-color: black; color:white; text-decoration:none; padding:5px;" href="Default.aspx">Back</a>
</body>
</asp:Content>

Contact.aspx.vb

Partial Class Contact

Inherits Page

Protected Sub buttonSubmit_Click(sender As Object, e As EventArgs)
    Dim sqlConnection1 As New Data.SqlClient.SqlConnection("ConnectionString HERE")

    Dim cmd As New Data.SqlClient.SqlCommand
    cmd.CommandType = System.Data.CommandType.Text
    cmd.CommandText = "INSERT INTO table_terms (Term, Definition, LongDescription, Category, DateLoaded, ProvidedBy, Department, SME) VALUES (Term, Definition, LongDescription, Category, DateLoaded, ProvidedBy, Department, SME)"
    cmd.Connection = sqlConnection1

    sqlConnection1.Open()
    cmd.ExecuteNonQuery()
    sqlConnection1.Close()

End Sub

End Class

任何提示都会很棒。非常感谢!

M.C.

【问题讨论】:

    标签: asp.net sql-server vb.net visual-studio-2015


    【解决方案1】:

    你的命令文本被抬高了。例如,字符串需要被引用。尝试在 SSMS 中运行该语句。如果它不能从那里工作,它就不能从 .Net 工作。

    另外,您所做的是一个安全问题。使用存储过程并将值作为参数传递。建立一个在 .Net 中执行的语句会导致更差的性能和注入漏洞。

    【讨论】:

    • 感谢@btberry 的反馈。应用这些更改
    猜你喜欢
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2014-02-02
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    相关资源
    最近更新 更多