2011-07-30 25 views
1

如果我使用文本框,我沒有問題獲取數據到SQL。我想用autowrap和所有的textarea。但是當我將<input type="text" id="au_id">更改爲<textarea name="au_id">時,我無法獲得下面列出的代碼.valuetextarea文本到SQL列

我只想交換多行textarea的單行文本框,仍然onclick讓我的行張貼在sql中。有些名稱/ ID對於他們的用途沒有任何意義,我從Microsoft網站複製了大部分代碼,並隨時進行更改。

代碼:

<%@ 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

要改變一個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> 

注意:刪除最大長度

要從VB.NET代碼訪問textarea的值:

Reponse.Write(au_id.value) 
+0

會試一試。感謝您及時的回覆。 –

+0

非常感謝你hamlin11。它的工作就像一個魅力。 –

+2

@Connie Chichuk - 請務必接受hamlin11的回答,以便獲得信用(和代表)。 :) – Tim

0

在有這個問題我自己,花一些時間試圖修復與許多許多論壇帖子我設法解決方案的問題這個同樣的問題。在所有的論壇中,我檢查了問題是從textarea獲取字符串值到一個sql INSERT聲明使用$_POST php超全局變量

我可以看到實際工作,並沒有拋出任何錯誤的唯一方法是忽略在編寫INSERT語句時使用列名。
因此,而不是:

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

簡單地使用:

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

唯一的缺點是你必須再爲表中的所有列列表值,但它修復得到textarea的價值的問題到一個SQL列。

希望這可以幫助