Search code examples
c#asp.net-mvcwcfdatabase-connectionlayered

Saving to DATABASE MVC pattern


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();
        }
    }
}

Solution

  • 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.