textarea文本转换为sql列

时间:2011-07-30 20:08:12

标签: asp.net sql-server vb.net textarea

如果我使用文本框,我没有问题将数据导入sql。我想使用带有自动装配的textarea以及所有这些。但是当我将<input type="text" id="au_id">更改为<textarea name="au_id">时,我无法获得下面列出的.value au_id.Value代码。

我想要的只是交换多行textarea的单行文本框,仍然onclick让我的行在sql中发布。有些名字/ id对于它们的用途没有任何意义,我从微软的网站上复制了大部分代码,并在我去的时候进行了更改。

代码:

<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text"%>

<html>
<head>
<style type="text/css">
    #au_id
    {
        height: 103px;
    }
</style>
</head>
<script language="VB" runat="server" >
Dim MyConnection As SqlConnection

Sub Page_Load(Src As Object, e As EventArgs)
    ' Create a connection to the "EMR" SQL database located on 
  ' the local computer.
    MyConnection = New SqlConnection("server=localhost;" _
       & "database=EMR;Trusted_Connection=Yes")
    ' Check whether this page is a  postback. If it is not 
    ' a  postback, call a custom BindGrid function.
    If Not IsPostBack Then
        BindGrid()
    End If
End Sub

' Implement an AddAuthor_Click function. This function does some data 
' validation on the input form and builds a parameterized command containing 
' all the fields of the input form.  Then it executes this command to the 
' database and tests (using the try command) whether the data was added. 
' Finally, it rebinds the DataGrid to show the new data.
Sub AddAuthor_Click(ByVal Sender As Object, ByVal e As EventArgs)
    Dim myCommand As SqlCommand
    Dim insertCmd As String

    If (au_fname.Value = "" Or au_lname.Value = "" _
       Or phone.Value = "") Then
        Message.InnerHtml = "ERROR: Null values not allowed for " _
           & "Author ID, Name or Phone"
        Message.Style("color") = "red"
        BindGrid()
        Exit Sub
    End If
    ' Build a SQL INSERT statement string for all the input-form
    ' field values.
    insertCmd = "insert into VisitData values (@Subjective, @Objective, @Assessment," _
       & "@Plan, @HT, @WT, @BP, @ServiceDate, @Diagnosis);"
    ' Initialize the SqlCommand with the new SQL string.
    myCommand = New SqlCommand(insertCmd, myConnection)
    ' Create new parameters for the SqlCommand object and
    ' initialize them to the input-form field values.
    myCommand.Parameters.Add(New SqlParameter("@Subjective", _
       SqlDbType.VarChar, 8000))
    myCommand.Parameters("@Subjective").Value = au_id.Value
    myCommand.Parameters.Add(New SqlParameter("@Objective", _
       SqlDbType.VarChar, 8000))
    myCommand.Parameters("@Objective").Value = au_lname.Value
    myCommand.Parameters.Add(New SqlParameter("@Assessment", _
       SqlDbType.VarChar, 8000))
    myCommand.Parameters("@Assessment").Value = au_fname.Value
    myCommand.Parameters.Add(New SqlParameter("@Plan", _
       SqlDbType.Char, 8000))
    myCommand.Parameters("@Plan").Value = phone.Value
    myCommand.Parameters.Add(New SqlParameter("@HT", _
       SqlDbType.VarChar, 40))
    myCommand.Parameters("@HT").Value = address.Value
    myCommand.Parameters.Add(New SqlParameter("@WT", _
       SqlDbType.VarChar, 20))
    myCommand.Parameters("@WT").Value = city.Value
    myCommand.Parameters.Add(New SqlParameter("@BP", _
       SqlDbType.Char, 10))
    myCommand.Parameters("@BP").Value = state.Value
    myCommand.Parameters.Add(New SqlParameter("@ServiceDate", _
       SqlDbType.Char, 10))
    myCommand.Parameters("@ServiceDate").Value = zip.Value
    myCommand.Parameters.Add(New SqlParameter("@Diagnosis", _
       SqlDbType.VarChar, 20))
    myCommand.Parameters("@Diagnosis").Value = contract.Value
    myCommand.Connection.Open()
    ' Test whether the new row can be added and  display the 
    ' appropriate message box to the user.
    Try
        myCommand.ExecuteNonQuery()
        Message.InnerHtml = "<b>Record Added</b><br>"
    Catch ex As SqlException
        If ex.Number = 2627 Then
            Message.InnerHtml = "ERROR: A record already exists with " _
               & "the same primary key"
        Else
            Message.InnerHtml = "ERROR: Could not add record, please " _
               & "ensure the fields are correctly filled out"
            Message.Style("color") = "red"
        End If
    End Try

    myCommand.Connection.Close()
    BindGrid()
End Sub

' BindGrid connects to the database and implements a SQL 
' SELECT query to get all the data in the "Authors" table 
' of the database.
Sub BindGrid()
    Dim myConnection As SqlConnection
    Dim myCommand As SqlDataAdapter
    ' Create a connection to the "EMR" SQL database located on 
    ' the local computer.
    myConnection = New SqlConnection("server=localhost;" _
       & "database=EMR;Trusted_Connection=Yes")
    ' Connect to the SQL database using a SQL SELECT query to get all 
    ' the data from the "Authors" table.
    myCommand = New SqlDataAdapter("SELECT * FROM VisitData", _
       myConnection)
    ' Create and fill a new DataSet.
    Dim ds As DataSet = New DataSet()
    myCommand.Fill(ds)
    ' Bind the DataGrid control to the DataSet.  will remain hidden 
    MyDataGrid.DataSource = ds
    MyDataGrid.DataBind()
 End Sub
</script>
<body style="font: 10pt verdana">
<form id="Form1" runat="server">
<h3><font face="Verdana">Inserting Visit Data</font></h3>
<table width="95%">
  <tr>
    <td valign="top" nowrap="nowrap">
      <ASP:DataGrid id="MyDataGrid" runat="server"
        Width="700"
        BackColor="#ccccff" 
        BorderColor="black"
        ShowFooter="false" 
        CellPadding=3 
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
        Visible="false"
        EnableViewState="false"

      />
    </td>
<body style="font: 10pt verdana">
    <td valign="top">
      <table style="font: 8pt verdana">
        <tr>
          <td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana">
            Add Visit Info:</td>
        </tr>
         <td>Subjective: </td>
          <td>
          <input type="text" id="au_id" value="" runat="server" maxlength="8000" 
                  style="overflow: scroll; white-space: normal;" />

          </td>
          <tr>
          <td nowrap>Objective: </td>
          <td><input type="text" id="au_lname" value="" 
            runat="server"></td>
        </tr>
        <tr nowrap>
          <td>Assessment: </td>
          <td><input type="text" id="au_fname" value="" 
            runat="server"></td>
        </tr>
        <tr>
          <td>Plan: </td>
          <td><input type="text" id="phone" value="" 
            runat="server"></td>
        </tr>
        <tr>
          <td>HT: </td>
          <td><input type="text" id="address" 
            value="" runat="server"></td>
        </tr>
        <tr>
          <td>WT: </td>
          <td><input type="text" id="city" value="" 
            runat="server"></td>
        </tr>
        <tr>
          <td>BP: </td>
          <td>
            <input type = "text" id="state" runat="server">
          </td>
        </tr>
        <tr>
          <td nowrap>Date of Service: </td>
          <td><input type="text" id="zip" value=""
             runat="server"></td>
        </tr>
        <tr>
          <td>Diagnosis: </td>
          <td>
          <Input type="text" id="contract" value="" runat="server">
          </td>
        </tr>
        <tr>
          <td></td>
          <td style="padding-top:15">
            <input id="Submit1" type="submit" OnServerClick="AddAuthor_Click"
              value="Add Visit" runat="server">
          </td>
        </tr>
        <tr>
          <td colspan="2" style="padding-top:15" align="center">
            <span id="Message" EnableViewState="false" 
              style="font: arial 11pt;" runat="server"/>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

要更改为textarea,您需要保留id="au_id"runat="server"属性

<input type="text" id="au_id" value="" runat="server" maxlength="8000" style="overflow: scroll; white-space: normal;" />

变为

<textarea runat="server" id="au_id" rows="4" cols="8">default text</textarea>

注意:删除maxlength

要从VB.NET代码访问textarea值:

Reponse.Write(au_id.value)

答案 1 :(得分:0)

我自己遇到这个问题,并花了一些时间试图解决许多论坛帖子的解决方案的问题,我设法解决了同样的问题。在我检查过的所有论坛中,问题是使用INSERT php超全局变量将textarea中的字符串值转换为sql $_POST语句

我能看到实际工作的唯一方法并没有引发任何错误,那就是在编写INSERT语句时忽略列名。
所以而不是:

"INSERT INTO tbl_dbTable ( Colummn1, Colummn2...) VALUES( '$_POST[textarea]', 'Value2'..)"

只需使用:

"INSERT INTO tbl_dbTable VALUES( '$_POST[textarea]', 'Value2'..)"

唯一的缺点是你必须列出表中所有列的值,但它解决了将textarea值转换为sql列的问题。

希望这会有所帮助