Solved

C# SQL Database Update question

Posted on 2003-11-25
2
335 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
ID: 9821296
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
ID: 9821334
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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