error on sqlserver database connection please rectify that

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 19
Reputation: krany18 is an unknown quantity at this point 
Solved Threads: 0
krany18 krany18 is offline Offline
Newbie Poster

error on sqlserver database connection please rectify that

 
0
  #1
Nov 11th, 2008
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9.  
  10. namespace WindowsApplication1
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. static SqlConnection cn = new SqlConnection("userid=sa;password=;database=northwind;server=localhost");
  19. SqlCommand cmd = new SqlCommand("select * from kkr", cn);
  20. SqlDataReader dr;
  21.  
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. if (cn.State = ConnectionState.Closed)
  25. {
  26. cn.Open();
  27. MessageBox.Show("ok");
  28. dr = cmd.ExecuteReader();
  29. label1.Text = dr.GetName(0);
  30. label2.Text = dr.GetName(1);
  31. label3.Text = dr.GetName(2);
  32. }
  33. else
  34. MessageBox.Show("connection is already opened:");
  35. }
  36.  
  37. private void button2_Click(object sender, EventArgs e)
  38. {
  39. if(dr.Read()==true)
  40. {
  41.  
  42. textBox1.Text = dr[0].ToString();
  43. textBox2.Text = dr[1].ToString();
  44. textBox3.Text = dr[2].ToString();
  45. }
  46. else
  47. {
  48. MessageBox.Show("No MOre Records:");
  49.  
  50. }
  51.  
  52.  
  53.  
  54. }
  55.  
  56. private void label1_Click(object sender, EventArgs e)
  57. {
  58.  
  59. }
  60.  
  61.  
  62.  
  63. }
  64.  
  65. }



Hai to All,

i written a code on c# dotnet using the sqlserver but it gives a error like that following

"Error 1 Property or indexer 'System.Data.Common.DbConnection.State' cannot be assigned to -- it is read only C:\examplesC#\WindowsApplication1\WindowsApplication1\Form1.cs 24 17 WindowsApplication1
Error 2 Cannot implicitly convert type 'System.Data.ConnectionState' to 'bool' C:\examplesC#\WindowsApplication1\WindowsApplication1\Form1.cs 24 17 WindowsApplication1
"
please recify my problem

A.kranti kumar
Last edited by Ancient Dragon; Nov 11th, 2008 at 7:58 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: error on sqlserver database connection please rectify that

 
0
  #2
Nov 11th, 2008
If you looked at the line of code its complaining about, its very true.

  1. if (cn.State = ConnectionState.Closed)

Your code is incorrect - please use code tags next time.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: error on sqlserver database connection please rectify that

 
0
  #3
Nov 11th, 2008
lol yes get your equals symbols in order

you are try to check its state, not set it
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 158
Reputation: vuyiswamb is an unknown quantity at this point 
Solved Threads: 5
vuyiswamb's Avatar
vuyiswamb vuyiswamb is offline Offline
Junior Poster

Re: error on sqlserver database connection please rectify that

 
0
  #4
Nov 14th, 2008
yes the guys are Right, it must look like this

  1. if (cn.State == ConnectionState.Closed)

Thanks
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 762 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC