Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:16:28+00:00 2026-06-12T18:16:28+00:00

namespace Hotel { public partial class Billing : Form { SqlConnection con = new

  • 0
namespace Hotel
{
    public partial class Billing : Form
    {
        SqlConnection con = new SqlConnection();
        SqlDataAdapter da;
        SqlCommand cmd = new SqlCommand();
        DataTable dt = new DataTable();
        public Billing()
        {
            InitializeComponent();
        }

        private void Billing_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Projects\\c# assignments\\Hotel Manager\\Hotel\\database\\master.mdf;Integrated Security=True;User Instance=True";
            //loadData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            int rno = Int32.Parse(txtRoom.Text);


            cmd.CommandText = "SELECT SUM(ItemRate) FROM logs WHERE RoomNo=" + rno +"";
            int amt = (int)cmd.ExecuteScalar();   //arror is at this part

       //ExecuteScalar: Connection property has not been initialized.

            cmd.CommandText = "INSERT INTO bill VALUES('" + txtBillNo.Text.ToString() + "','" + txtRoom.Text.ToString() + "','" + amt.ToString() + "')";
            con.Close();
            txtBillNo.Text = "";
            txtRoom.Text = "";
            BillView bv = new BillView();
            bv.ShowDialog();
        }
    }
}

please help me with this error i am not able to store the the SQL query result into a variable???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:16:30+00:00Added an answer on June 12, 2026 at 6:16 pm
    1. You are open for SQL-Injection. Don’t concatenate strings to build your query. Instead use SQL-Parameters.
    2. Use using-statement for you connection (and everything else implementing IDisposable). Dispose will also close the connection, with using even on error.
    3. The reason for the exception is that you don’t have initialized the connection of the SqlCommand since you have’t specified the connection. You can use the property or the appropriate constructor.

    Here’s an example:

    int amt;  
    using (var con = new SqlConnection(ConnectionString)) {
        var sql = "SELECT SUM(ItemRate) FROM logs WHERE RoomNo = @RoomNo";
        using (var cmd = new SqlCommand(sql, con)) {
            cmd.Parameters.AddWithValue("@RoomNo", Int32.Parse(txtRoom.Text));
            con.Open();
            amt = (int)cmd.ExecuteScalar();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

namespace myApplication { public partial class Form1 : Form { public Form1() { InitializeComponent();
namespace explorer { public partial class Form1 : Form { public Form1() { InitializeComponent();
Consider: namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() {
namespace KetBanBonPhuong.Controls.Default { public partial class SugFriends : System.Web.UI.UserControl { private string Uid; protected
namespace WebApplication1.Site { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender,
namespace foo; class a { private $bar; public $baz; protected $alpha } $reflect=new \ReflectionClass('a');
namespace MyApp.MyNamespace { public class MyClass:System.Web.UI.Page { private DataUtility Util = new DataUtility(); private
namespace X{ public static class URLs { public static TabController tabIdLookUp = new TabController();
namespace MyOldService { public MyNewService.AddressList ToPrivateAddressList() { MyNewService.AddressList privAddrList = new MyNewService.AddressList(); privAddrList.creator =
using namespace std; class A { public: A() {} ~A() {} map<int, string*>& getMap()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.