943,688 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1194
  • C# RSS
Nov 11th, 2008
0

error on sqlserver database connection please rectify that

Expand Post »
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krany18 is offline Offline
19 posts
since Nov 2008
Nov 11th, 2008
0

Re: error on sqlserver database connection please rectify that

If you looked at the line of code its complaining about, its very true.

C# Syntax (Toggle Plain Text)
  1. if (cn.State = ConnectionState.Closed)

Your code is incorrect - please use code tags next time.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Nov 11th, 2008
0

Re: error on sqlserver database connection please rectify that

lol yes get your equals symbols in order

you are try to check its state, not set it
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 14th, 2008
0

Re: error on sqlserver database connection please rectify that

yes the guys are Right, it must look like this

C# Syntax (Toggle Plain Text)
  1. if (cn.State == ConnectionState.Closed)

Thanks
Reputation Points: 31
Solved Threads: 14
Posting Whiz
vuyiswamb is offline Offline
310 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC