letting users who visit my website add records to a sql server database

Discussion in 'Databases' started by mrsdewinter2@, May 5, 2008.

  1. #1
    in visual studio web developer express edition 2008 i added a new item this was a sql server database which i called test2. I added a table called comments, and the fields name, email, comments, IPAddress, and Date and in my comments.aspx page i added a name textbox, a texbox for email, a textbox for comments and the submit button i double clicked on the submit button and added the following code

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim mydS As New SqlDataSource
    mydS.ConnectionString = ConfigurationManager.ConnectionStrings("testConnectionString1").ToString

    mydS.InsertCommandType = SqlDataSourceCommandType.Text

    mydS.InsertCommand = "INSERT INTO comments(Name,Email,Comments,IPAddress,Date) VALUES (@Name,@Email,@Comments,@IPAddress,@Date)"

    mydS.InsertParameters.Add("Name", TextBox1.Text)
    mydS.InsertParameters.Add("Email", TextBox2.Text)
    mydS.InsertParameters.Add("Comments", TextBox3.Text)
    mydS.InsertParameters.Add("Date", Date.Now)
    mydS.InsertParameters.Add("IPAddress", Request.Params("REMOTE_ADDR"))

    Dim rowsaffected As Int16 = 0
    Try
    rowsaffected = mydS.Insert
    Catch ex As Exception
    Server.Transfer("errors.aspx")
    Finally
    mydS = Nothing
    End Try
    If rowsaffected <> 1 Then
    Server.Transfer("errors.aspx")
    Else
    Server.Transfer("confirm.aspx")
    End If
    End Sub

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
    but when i run it it gives me a problem with this line mydS.ConnectionString = ConfigurationManager.ConnectionStrings("testConnectionString2).ToString and says something abt unidentified object and I want to know how do i set the connection string or alternatively where?????? and my table does not update with the corresponding values i enter on the webpage. i also changed the authentication mode to forms instead of windows as i was advised but i'm still having a problem


    and i've added this to the web config


    <appSettings>
    </appSettings>
    <connectionStrings>
    <add name="testConnectionString2" connectionString="Server=YOURSERVER;Database=DatabaseName;User ID=USERID;Password=PASSWORD;Trusted_Connection=False" providerName="System.Data.SqlClient"/>
    </connectionStrings>
     
    mrsdewinter2@, May 5, 2008 IP
  2. vasildb

    vasildb Well-Known Member

    Messages:
    845
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    118
    #2
    vasildb, May 5, 2008 IP