Results 1 to 6 of 6

Thread: damn sql...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Toronto, Ontario, Canada
    Posts
    275

    damn sql...

    Hey. I am working on a project in SQL Server & VB.NET. On the web form there is a single line textbox and a mutli-line textbox. When you click the button it’s supposed to update a entry in the SQL Database. But when I try using this code it won’t work, or it will but it will be all strange??!??!
    VB Code:
    1. Dim strRedirect As String
    2.         strRedirect = Request("uid")
    3.         Dim tempstringa As String = TextBox1.Text
    4.         Dim tempstringb As String = TextBox2.Text
    5.         Dim cmd As SqlCommand
    6.         Dim cnn As SqlConnection
    7.         cnn = New SqlConnection("server=localhost;user=dex2;pwd=password1;database=pocketdotnet;")
    8.         Dim tempstringc As String = "UPDATE " & Me.Session.Item("workgroup") & "News set title='%%%%', disc='%%%%%' where uid='" & strRedirect & "'"
    9.         tempstringc = Replace(tempstringc, "'%%%%'", "'" + Server.HtmlEncode(tempstringa) + "'")
    10.         tempstringc = Replace(tempstringc, "'%%%%%'", "'" + Server.HtmlEncode(tempstringb) + "'")
    11.         cmd = New SqlCommand(Server.HtmlEncode(tempstringc), cnn)
    12.         cnn.Open()
    13.         cmd.ExecuteNonQuery()
    14.         cnn.Close()

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    well, you don't need the HtmlEncode stuff. ADO.NET has good support for parameters in inline sql, so I'd use those instead of replacing stuff, makes your code a little more readable. Maybe try building your sql something like this:
    VB Code:
    1. Dim uid As String = Request.QueryString.Item("uid")
    2. Dim connString As String = "server=localhost;user=dex2;pwd=password1;database=pocketdotnet;"
    3. Dim cn As New SqlConnection(connString)
    4. Dim cmdText As New StringBuilder
    5. cmdText.Append(String.Format("Update {0}News Set", Me.Session.Item("workgroup").ToString()))
    6. cmdText.Append("title = @title ")
    7. cmdText.Append(", disc = @disc ")
    8. cmdText.Append("Where uid = @uid")
    9. Dim cmd As New SqlCommand(cmdText.ToString(), cn)
    10. cmd.Parameters.Add(New SqlParameter("@title", TextBox1.Text))
    11. cmd.Parameters.Add(New SqlParameter("@disc", TextBox2.Text))
    12. cmd.Parameters.Add(New SqlParameter("@uid", uid))

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Toronto, Ontario, Canada
    Posts
    275
    That works well but it doesn’t work if one of the textbox’s contains multiple lines.

    Thanks!

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Somethin else must be wrong cuz that does work with multiline textboxes. Here's a little tester:
    VB Code:
    1. <%@ Import Namespace="System.Data" %>
    2. <%@ Import Namespace="System.Data.SqlClient" %>
    3. <%@ Import Namespace="System.Text" %>
    4. <script language="vb" runat="server">
    5. Protected Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    6.     Dim connString As String = "user id=sa;password=sa;database=Scratch;server=DeathAngel;"
    7.     Dim cn As New SqlConnection(connString)
    8.     Dim cmdText As New StringBuilder()
    9.     cmdText.Append ("Insert Into MultiLine ( value ) Values ( @value )")
    10.     Dim cmd As New SqlCommand(cmdText.ToString(), cn)
    11.     cmd.Parameters.Add(new SqlParameter("@value", TextBox1.Text))
    12.     cmd.Connection.Open()
    13.     cmd.ExecuteNonQuery()
    14.     cmd.Connection.Close()
    15. End Sub
    16. </script>
    17. <html>
    18.     <body>
    19.         <form runat="server">
    20.             <asp:TextBox ID="TextBox1" Runat="server"
    21.                 TextMode="MultiLine" Columns="40" Rows="10"/><br/>
    22.             <asp:Button ID="btnSave" Runat="server" Text="Save" OnClick="btnSave_Click"/>
    23.         </form>
    24.     </body>
    25. </html>

    and the table being inserted into looks like this:
    Code:
    create table MultiLine
    (
    		[id] int not null identity primary key
    	,	value varchar ( 200 )
    )

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Toronto, Ontario, Canada
    Posts
    275
    Insert works just fine, it's update that is not working!?!?!

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    hmmm, changed to an update and it worked fine:
    VB Code:
    1. cmdText.Append ("Update MultiLine Set value = @value where [id] = 1")
    Maybe post the new update code that's not working.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width