2016-08-24 36 views
-2

我是VB.net的新手,我正在开发Visual Studio 2015中的项目。在我的应用程序的选项卡之一中,我尝试通过表单发布将新记录插入到SQL表中,但是一旦我点击按钮,什么都不会发生,并且不会添加新记录。我目前的代码如下,任何帮助将是伟大的!感谢:通过vb.net发布数据到SQL表格使用onClick Submit按钮的形式

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.

回答

0

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

此外,你在做什么是一个安全问题。使用存储过程并将值作为参数传递。建立一个在.Net中执行的声明将导致更糟的性能和注入漏洞。

+0

感谢您的反馈@btberry。应用这些更改 –