using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static SqlConnection cn = new SqlConnection("userid=sa;password=;database=northwind;server=localhost");
        SqlCommand cmd = new SqlCommand("select * from kkr", cn);
        SqlDataReader dr;

        private void button1_Click(object sender, EventArgs e)
        {
            if (cn.State = ConnectionState.Closed)
          {
                cn.Open();
                MessageBox.Show("ok");
                dr = cmd.ExecuteReader();
                label1.Text = dr.GetName(0);
                label2.Text = dr.GetName(1);
                label3.Text = dr.GetName(2);
            }
            else
                MessageBox.Show("connection is already opened:");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(dr.Read()==true)
            {

                textBox1.Text = dr[0].ToString();
                textBox2.Text = dr[1].ToString();   
                textBox3.Text = dr[2].ToString();
            }
                else
                {
                    MessageBox.Show("No MOre Records:");

                }


            
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }



        }

    }

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

Recommended Answers

All 3 Replies

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

if (cn.State = ConnectionState.Closed)

Your code is incorrect - please use code tags next time.

lol yes get your equals symbols in order

you are try to check its state, not set it

yes the guys are Right, it must look like this

if (cn.State == ConnectionState.Closed)

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.