Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

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

Options
  • 25-02-2007 7: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 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 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