CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: ado.net

  1. #1
    Join Date
    Jul 2008
    Posts
    11

    ado.net

    SqlConnection myconnection = new SqlConnection("server=RHGVR-1667F3F5E;Database=vanamu;Trusted_Connection=yes;");

    myconnection.Open();

    SqlCommand mycommand = new SqlCommand("insert Usedetails ( Personname, Address, City, State, Zip) values( Rami, lutz hall, commerce, texas, 75429)");

    mycommand.ExecuteNonQuery();

    myconnection.Close()

    In the above code plz correct the store procedure that i have used there. and also the connection strings. Iam getting errors.

  2. #2

    Re: ado.net

    Several things:

    - It's best to provide the actual text of the errors you're getting. Your code may be correct but the environment may have issues. The actual error messages help decipher that.

    - Please make sure to use code tags around your code, it makes things much more readable.

    - You're not using a stored procedure, that's an ad hoc query. You need to add ' marks around each of your string values.

    - For your connection issues, refer to www.connectionstrings.com for examples of connecting to assorted databases.

  3. #3
    Join Date
    Dec 2007
    Location
    South Africa
    Posts
    263

    Resolved Re: ado.net

    No you code is not Correct , mmetzger is right, Show us the Whole code, but now your Conection string is incorrect.

    Your Connection string should look like this, Change it from

    Code:
    SqlConnection myconnection = new SqlConnection("server=RHGVR-1667F3F5E;Database=vanamu;Trusted_Connection=yes;");
    to this

    Code:
    SqlConnection myconnection = new SqlConnection("User ID=sa;Password=hello; Server=RHGVR-1667F3F5E;Database=vanamu;Trusted_Connection=yes");
    Another thing i saw is that you open the connection in a wrong place. You must open the connection when you are about to execute something. That means the code that you just posted to us should look like this .

    Code:
    SqlConnection myconnection = new SqlConnection("User ID=sa;Password=hello; Server=RHGVR-1667F3F5E;Database=vanamu;Trusted_Connection=yes");
    
    
    SqlCommand mycommand = new SqlCommand("insert Usedetails ( Personname, Address, City, State, Zip) values( Rami, lutz hall, commerce, texas, 75429)");
    
    try
    {
    myconnection.Open();
    
    mycommand.ExecuteNonQuery();
    }
    catch(SQlexception)
    {
         Finnaly
      {
          myconnection.Close();
      }
    }.....
    , i did not do this in Vs , I hope you see the idea. Do not write untidy code , because it might Confuse you too

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

Featured