Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Insert value into SQL Database from business layer

Posted on 2010-11-20
3
Medium Priority
?
414 Views
Last Modified: 2012-05-10
I'm creating a C# application using a three tiered approach. I've passed my string variable(Rating) from the presentation layer to the business layer now I need to pass it to the data access layer and insert it into my sql database. I have no idea how where to begin.
//Busness layer 
public BLL()
        {
        }

        //Properties
        public string Rating
        {
            get
            {
                return rating;
            }
            set
            {
                rating = value;
            }
          }
        }

//Data access layer
        public int Save(string sql) 
        {
            string sqlIns = "INSERT INTO table (Rating) VALUES (@Rating)";
            conn.Open();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            return cmd.ExecuteNonQuery();

Open in new window

0
Comment
Question by:98fatboyrider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 total points
ID: 34181068
Normally you would pass a business object between the layers, or have a method in your DAL that accepts parameters from a business object (depending on what pattern you are using).

It's not clear from the code you have posted how the Rating property fits into your overall structure. However, in very simply terms, if you are saving just the rating by itself then you would change the method in your DLL to something like:
public void Save(string rating)
{
     string sqlIns = "INSERT INTO table (Rating) VALUES (@Rating)";
     
     SqlCommand cmd = new SqlCommand(sqlIns, conn);
     cmd.Parameters.AddWithValue("@Rating", rating);

     try
    {
        conn.Open();
        cmd.ExecuteNonQuery();
    }
    catch
   {
        // do something with the exception
   }
    finally
    {
           if (conn.ConnectionState != ConnectionState.Closed)
               conn.Close();
    }
}

Open in new window


You might the have a Save() method in your BLL to save the data:
    // in BLL
    public void Save()
    {
           DAL dal = new DAL();
           dal.Save(this.Rating);
    }

Open in new window


Just a simple example based on what I could glean from your current code. But hopefully it will provide you with a starting point.
0
 

Author Comment

by:98fatboyrider
ID: 34184178
I believe I'm close but not quite there yet. All I need to do is pass a textbox value to the business layer then insert it into my database from the data access layer. Can you see where I’m going wrong?.
//Presentation layer
private void btnAddRating_Click(object sender, EventArgs e)
        {
            string Rating = txtNewRating.Text.ToUpper();
            BLL setRating = new BLL();
            Rating = setRating.ToString();
            setRating.Save();
        }
//Busness layer
public class BLL
    {
        private string _rating;
        //Default constructor
        public BLL()
        {
        }
        //Properties
        public string Rating
        {
            get
            {
                return _rating;
            }
            set
            {
                _rating = value;
            }
        }
public void Save()
        {
            DAL dal = new DAL();
            dal.Save(this.Rating);
        }
  }
//Data access layer

       //Getting all ratings from ratings table
        public DataSet getRatings(string sql)
        {
            // Connection and SQL strings
            string SQL = "SELECT * FROM tblRating";
            try
            {
                // create a connection object
                string ConnectionString = "Data Source=.\\SQLExpress;Integrated Security=true; AttachDbFilename=D:\\MoviesDB\\MovieDB.mdf;User Instance=true;";
                SqlConnection conn = new SqlConnection(ConnectionString);
                // create command object
                SqlCommand cmd = new SqlCommand(SQL, conn);
                DataSet ds = new DataSet();
                // open connection
                conn.Open();
                // call command's ExcuteReader
                SqlDataAdapter da = new SqlDataAdapter(SQL, conn);
                da.Fill(ds);
                return ds;
           }
            finally
            {
                conn.Close();
            }
        }

 public void Save(string rating)
{
     string sqlIns = "INSERT INTO table (Rating) VALUES (@Rating)";
     
     SqlCommand cmd = new SqlCommand(sqlIns, conn);
     cmd.Parameters.AddWithValue("@Rating", rating);

     try
     {
         conn.Open();
         cmd.ExecuteNonQuery();
     }
     catch
     {
         // do something with the exception
     }
     finally
     {
         if (conn.ConnectionState != ConnectionState.Closed)
             conn.Close();
     }
    }
   }

Open in new window

0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 1000 total points
ID: 34184214
This block is incorrect:
            BLL setRating = new BLL();
            Rating = setRating.ToString();
            setRating.Save()

Open in new window

It should be something more like:
            BLL setRating = new BLL();
            setRating.Rating = Rating;
            setRating.Save()

Open in new window

0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question