1

I am fairly new to programming so please bear with me. I have written following code in different classes throughout my project, and I am wondering why my input does not get stored in my database. I really hope someone can help me, since I have been struggling with this problem for a few days now, scavenging the darkest corners of the web.

First of all I should tell you that my application is also using WCF - therefore the methods should be called through the WCFService to the Client. Any help would be greatly appreciated!

PS: If it helps here is a screenshot of my Solution overview: http://imgur.com/JV9Xl3K

Client Code:

  using Client.WCFServiceRef;  //My wcf Service reference

namespace Client
{
    public partial class MainWindow : Window
    {
        private WCFServiceClient Client;
        public MainWindow()
        {
            InitializeComponent();
            Client = new WCFServiceClient();
        }
        public string getMovieName()
        {
            string movieName = txtName.Text;
            return movieName;
        }
        public string getMovieLength()
        {
            string movieLength = txtLength.Text;
            return movieLength;
        }
        public string getMovieDesc()
        {
            string movieDesc = txtDescription.Text;
            return movieDesc;
        }
        private void btnMovies_Click(object sender, RoutedEventArgs e)
        {
            Client.addMovie(getMovieName(), getMovieLength(), getMovieDesc());
        }
    }
}

My MovieController code:

using Server.DB;

namespace Server.Control
{
    class MovieCtr
    {
        public void addMovie(string movieName, string movieLength, string movieDesc)
        {
            DBMovie conObj = new DBMovie();
            conObj.addMovie(movieName, movieLength, movieDesc);
        }
    }
}

My DBMovie Class

namespace Server.DB
{
    public class DBMovie
    {
        DBConnection dbCon = new DBConnection();
        public void addMovie(string movieName, string movieLength, string movieDesc)  
        {
            dbCon.openConnection();
            SqlCommand com = new SqlCommand();
            string query = "INSERT into movies (Name, Runtime, Description) VALUES  ('" + movieName + "','" + movieLength + "','" + movieDesc + "');"; 
            com.CommandText = query;


        }
    }
}

Finally my dbconnection class

namespace Server.DB
{
    public class DBConnection
    {
        SqlConnection sc = new SqlConnection();
        public void openConnection()
        {
            try
            {
                sc.ConnectionString = ("Data Source=balder.ucn.dk;Initial Catalog=dmaa0213_6;User ID=dmaa0213_6;password=XXXXXX");
                sc.Open();
            }  //endTry
            catch (SqlException ex)
            {
                Console.WriteLine("Could not open connection to database");
                Console.WriteLine(ex);
            }
        }
        public void closeConnection()
        {
            sc.Close();
        }
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Matt Baech
  • 404
  • 11
  • 23

1 Answers1

1

You need to call the ExecuteNonQuery on the SqlCommand object.

string query = "INSERT into movies (Name, Runtime, Description) VALUES  ('" + movieName + "','"    + movieLength + "','" + movieDesc + "');"; 
com.CommandText = query;
com.ExecuteNonQuery();

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

One more thing, you need to associate your connection to the command object. The example in the web page above has the SqlConnection object being passed into the SqlCommand constructor. You would need to rewrite the code for this to work properly.

bytedreamer
  • 142
  • 8
  • That did seem to do something, but now results in the application crashing when the button is clicked. (guess that's better than before where the button didn't seems to do much. – Matt Baech May 17 '14 at 15:26
  • The problem is, as stated, I am really new to programming, and not quite sure what you mean by that. – Matt Baech May 17 '14 at 15:53
  • For an example, check out this article. It is important to make sure that the using statement is being used properly. It will ensure that the SQL connection is properly disposed when it is completed. (http://www.hanselman.com/blog/WhyTheUsingStatementIsBetterThanASharpStickInTheEyeAndASqlConnectionRefactoringExample.aspx) – bytedreamer May 17 '14 at 16:03
  • The problem is that I don't understand exactly what the USING does in that context. I only know how to use it in the beginning of a class – Matt Baech May 17 '14 at 17:44
  • If a object implements the IDisposable interface, then the Dispose method needs to be called to release unmanaged resources. The USING keyword ensure that Dispose method is called when it goes out of scope. It is a shortcut to having a use a try/catch/finally. (http://stackoverflow.com/questions/5563886/close-sqlconnection-in-the-finally-when-using-using) Both SqlConnection and SqlCommand implements the IDisposable interface. – bytedreamer May 17 '14 at 17:54
  • Okay, thanks, I guess I have some reading to do. I am also not sure about what the IDisposable interface does, other than it having to do with the method dispose() which I again don't know how to use. I believe you see my current level of programming. :) Will do some research. Will not waste your time anymore. I appreciate your help so far, and wish you a good evening. – Matt Baech May 17 '14 at 20:01