Results 1 to 16 of 16

Thread: Insert to SQL(resolved Mend rawks)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Resolved Insert to SQL(resolved Mend rawks)

    Okay finally got my first webapp up and its static, has no sql connections. I have been reading and looking at the forum for how to connect to a database. What I want to do is create a webform, fill in a textbox, click the button to save and have it write to the table on the SQL server. My server is called webserver, my database is webtest and my table is answer and the field is answer1. From what I have read it seems you need to put postback code in the form load event, create a datatable from all the data on the sql server, add a datarow and then do an update from the sqladapter. This seems like alot of bandwith usage for a simple update or am I barking up the wrong tree. I don't care what the data is thats there already, I just want to add a new row.
    Last edited by Beast777; Mar 2nd, 2006 at 11:02 AM. Reason: Resolved

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Insert to SQL

    ADO.NET QUickstart:

    http://samples.gotdotnet.com/quickst...sOverview.aspx

    No, it does not take up a lot of bandwidth because datasets (which contain datatables) are disconnected. In other words, the dataadapter fills up the dataset, and the dataset is just a collection that exists in memory. There is no connection. An update reopens a connection and sends values back to the databsae.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Insert to SQL

    I had a look that seems to be just the basic run down on ADO.NET. Can I not just use the same code I use in a windows app like this:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim strConnString As String = "workstation id=""S-KG_-CFSCE-2098"";packet size=4096; integrated security=SSPI;data source=""WEBSERVER"";persist security info=False;initial catalog=Webtest"
    3.         Dim SqlConn As New SqlClient.SqlConnection(strConnString)
    4.         'Dim strsql As String = "INSERT into Answers(Answer1) values (@Answer)"
    5.           Dim sqlcmd As New SqlClient.SqlCommand(strsql, SqlConn)
    6.         Try
    7.             sqlcmd.Parameters.Add("@Answer", TextBox1.Text)
    8.             SqlConn.Open()
    9.             sqlcmd.ExecuteNonQuery()
    10.  
    11.         Catch ex As Exception
    12.  
    13.         End Try
    14.     End Sub

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Insert to SQL

    Yes, but that is for non queries. If you do a SELECT, then you do need to use a dataset.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Insert to SQL

    SO if I am just inserting the above should work? because when I click on the button it gives no error but does not insert.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Insert to SQL

    Maybe I should tell you what the app does and that my clarify what method should be used. It will be an online questionaire that users fill out, click send and it puts the info into the database. Users will not ever edit it.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Insert to SQL

    Step through your code and see if it's hitting the exception.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Insert to SQL

    It has a {System.Data.SqlClient.SqlException} with no more info, occurs during the open. Could it be not the code but access rights? I added ASPNET and IUSR accts to sql and to this database and gave them db_owner rights.

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Insert to SQL

    In your catch,

    do a Response.Write ex.Message

    (or just do a quickview of ex)

    and look at its message property.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Insert to SQL

    Login failed for user null. Reason not associated with a trusted SQL server connection

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Insert to SQL

    Have you set identity impersonate = true in the ASP.NET App?

    Does the logged in user (you?) have permission to access the sql server database?

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Insert to SQL

    I just did a quick google, do I need to add Trusted_Connection=yes somewhere?

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Insert to SQL

    No I did not do identity impersonate, I am knew to this. That goes into the web config if I remember what I read somewhere. Does it matter where it goes in that file?

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Insert to SQL

    I added <identity impersonate="true"></identity> and still same error.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Insert to SQL

    Guess I am pretty new at this. The whole time I have been changing the rights on the live server when I should of been changing the rights on my local web machine. Sorry for all the trouble and thanks for all the help. Mend, you are my the best.

  16. #16
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Insert to SQL

    No problem, we've all been there.


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