Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C# Q. Reading and writing from a DB to textboxes.

  • 25-02-2007 07:45PM
    #1
    Closed Accounts Posts: 1,034 ✭✭✭


    Hi folks, I am getting an error when I attempt to save data from a selection of textboxes into a SQL server 2005 DB. I am using visual studio 2005, c#. Here is a sample of my code:

    //Add entered data into Remote Access Table(tblRemoteAccessDetails)
    protected void btnSaveRemoteAccessDetails_Click(object sender, EventArgs e)
    {
    string szSQL;
    szSQL = string.Format("INSERT INTO tblRemoteAccessDetails (RemoteAccessID, RemoteAccessIP, RemoteAccessmethod, RemoteAccessports, RemoteAccessUsername, RemoteAccesspassword, RemoteAccesscomments) VALUES({0}, {1}, '{1}', {1}, '{1}', '{1}', '{1}'",
    //Where to define ExecuteSQL?
    txtNewRemoteAccessID.Text,
    txtNewRemoteAccessIp.Text,
    txtNewRemoteAccessMethod.Text,
    txtNewRemoteAccessPorts.Text,
    txtNewRemoteAccessUsername.Text,
    txtNewRemoteAccessPassword.Text,
    txtNewRemoteAccessComments.Text);


    connection.ExecuteSQL(szSQL);

    Now, my question is I am getting the error:
    Error 109 'System.Data.SqlClient.SqlConnection' does not contain a definition for 'ExecuteSQL'

    Where do I define this method? In the web.config file? If so any ideas on how to do so, I feel if I get this error sorted I will be close to having my project working. I have tried MSDN help and various books but they are of little help with this error.

    Thanks for the feedback.


Comments

  • Registered Users, Registered Users 2 Posts: 348 ✭✭SonOfPerdition


    Hi astraboy,

    your object connection is of type System.Data.SqlClient.SqlConnection.

    have you checked to see if that type has a method called "ExecuteSQL"?

    That type is responsible for maintaining a connection. I'm pretty sure it doesn't have a method called ExecuteSQL which is what the error is telling you.

    If you google "System.Data.SqlClient.SqlConnection" you should find sample code on the web on how to create a connection, how to form a query and call the query passing it the connection.

    read this page
    http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.aspx

    you should be interested in the class 'SQLCommand' along with 'SQLConnection'.

    http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

    hint .. have a read of the ExecuteNonQuery method.


    hope that helps you progress further! :)

    SOP


  • Registered Users, Registered Users 2 Posts: 151 ✭✭sailorfoley


    SQLConnection object does not have a method called ExecuteSQL. To execute an sql statement on an SQLConnection object you need to create an SQLCommand object.

    This object has a property property called Connection. Set this property to be your existing SQLConnection.

    Set the CommandText parameter of the SQLCommand object to be your string with the sql in it.

    Then call ExecuteNonQuery.

    This should be all you need.


Advertisement