Solved

C# SQL Database Update question

Posted on 2003-11-25
2
328 Views
Last Modified: 2010-04-16
I have the following code.  It works except for the SQL UPDAT E part.  I get an unhandeled exception error that says I need an open and available connection.  Any help would be appreciated.

I am using access and I am using Borland C# builder to compile this if that information makes a difference.

Thanks,
Travis

private void Button1_Click(object sender, System.EventArgs e)
            {
                  OleDbConnection cn = new OleDbConnection("Provider=sqloledb;Data Source=(local);Initial Catalog=BoulderLodging;User Id=boulderlodging;Password=lodge4rent");
                  OleDbCommand cmd = new OleDbCommand("insert_deal", cn);
                  cmd.CommandType = CommandType.StoredProcedure;

                  cmd.Parameters.Add("@hotel_id", System.Data.OleDb.OleDbType.BigInt).Value = int.Parse(DropDownList1.SelectedItem.Value);
                  cmd.Parameters.Add("@deal_price",System.Data.OleDb.OleDbType.VarChar, 50).Value = deal_price.Text;
                  cmd.Parameters.Add("@deal_title", System.Data.OleDb.OleDbType.VarChar, 50).Value = deal_title.Text;
                  cmd.Parameters.Add("@deal_avail", System.Data.OleDb.OleDbType.VarChar, 350).Value = deal_avail.Text;
                  cmd.Parameters.Add("@deal_desc", System.Data.OleDb.OleDbType.VarChar, 1500).Value = deal_desc.Text;

                  cmd.ExecuteNonQuery();
                  cmd.Dispose();

                  string commandText = "Update hotels Set great_deals=1 Where hotel_id ="+int.Parse(DropDownList1.SelectedItem.Value);
                  OleDbCommand cmd2 = new OleDbCommand(commandText, cn);
                  cmd2.ExecuteNonQuery();

                  cmd.Dispose();
                  cn.Close();

                  Response.Redirect("index.aspx");
0
Comment
Question by:gouber
2 Comments
 

Accepted Solution

by:
Shriraj earned 130 total points
Comment Utility
i Don't think that you don't know this, but you forget to open Sql Connection.

add,
cn.Open();

before  cmd.ExecuteNonQuery();

If this not your question, i am really sorry for this answer.
0
 
LVL 1

Author Comment

by:gouber
Comment Utility
I guess it is just the holidays creeping up on me.  There is no way I can focus at work :S.  That was part of the prob.  I guess that there are just some insert errors.  I cant believe that I took out the open connection.  Oh well.

Thanks,
Travis
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now