Click here to Skip to main content
13,196,946 members (54,541 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
hai
i am a student working on the project of obtaining the data fro the msql database,i tried to write the code in c# for windows application but i receive some errors in that so please help me with those errors

code is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        //SqlConnection con = new SqlConnection( "server=localhost;user id=root;password=******;database=test");//
        SqlConnection con = new SqlConnection();
 
        public Form1()
        {
            InitializeComponent();
        }
 

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
               // con.ConnectionString = "server=localhost;user id=root;password=*****;database=test";
 
                con.ConnectionString = "server=.\\sqlexpress;database=test;integrated security=true;";
 
                SqlDataAdapter adp = new SqlDataAdapter("SELECT Date_time,CT1_AH,CT2_AH,CT3_AH,CT1_WattHr,CT2_WattHr,Max_Temperature,Min_Temperature,Volt1,Amp1,Volt2,Batt1_percentFull,Batt1_gotcharged,Batt2_percentFull,Batt2_gotcharged FROM periodicdata WHERE Date_time BETWEEN '" + dateTimePicker1.Value.ToShortDateString() + "' AND '" + dateTimePicker2.Value.ToShortDateString() + "'", con);
                DataSet ds = new DataSet();
                adp.Fill(ds, "s");
                if (ds.Tables["s"].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables["s"].Rows.Count; i++)
                    {
                        dataGridView1.Rows.Add();
                        dataGridView1.Rows[i].Cells[0].Value = ds.Tables["s"].Rows[i].ItemArray[0];
                        dataGridView1.Rows[i].Cells[1].Value = ds.Tables["s"].Rows[i].ItemArray[1];
                        dataGridView1.Rows[i].Cells[2].Value = ds.Tables["s"].Rows[i].ItemArray[2];
                        dataGridView1.Rows[i].Cells[3].Value = ds.Tables["s"].Rows[i].ItemArray[3];
                        dataGridView1.Rows[i].Cells[4].Value = ds.Tables["s"].Rows[i].ItemArray[4];
                        dataGridView1.Rows[i].Cells[5].Value = ds.Tables["s"].Rows[i].ItemArray[5];
                        dataGridView1.Rows[i].Cells[6].Value = ds.Tables["s"].Rows[i].ItemArray[6];
                        dataGridView1.Rows[i].Cells[7].Value = ds.Tables["s"].Rows[i].ItemArray[7];
                        dataGridView1.Rows[i].Cells[8].Value = ds.Tables["s"].Rows[i].ItemArray[8];
                        dataGridView1.Rows[i].Cells[9].Value = ds.Tables["s"].Rows[i].ItemArray[9];
                        dataGridView1.Rows[i].Cells[10].Value = ds.Tables["s"].Rows[i].ItemArray[10];
                        dataGridView1.Rows[i].Cells[11].Value = ds.Tables["s"].Rows[i].ItemArray[11];
                        dataGridView1.Rows[i].Cells[12].Value = ds.Tables["s"].Rows[i].ItemArray[12];
                        dataGridView1.Rows[i].Cells[13].Value = ds.Tables["s"].Rows[i].ItemArray[13];
                        dataGridView1.Rows[i].Cells[14].Value = ds.Tables["s"].Rows[i].ItemArray[14];
                    }
                }
                else
                {
                    MessageBox.Show("No Record Found For Given Date.");
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns.Add("", "Date Time");
            dataGridView1.Columns.Add("", "CT1_AH");
            dataGridView1.Columns.Add("", "CT2_AH");
            dataGridView1.Columns.Add("", "CT3_AH");
            dataGridView1.Columns.Add("", "CT1_WattHr");
            dataGridView1.Columns.Add("", "CT2_WattHr");
            dataGridView1.Columns.Add("", "Max_Temperature");
            dataGridView1.Columns.Add("", "Min_Temperature");
            dataGridView1.Columns.Add("", "Volt1");
            dataGridView1.Columns.Add("", "Amp1");
            dataGridView1.Columns.Add("", "Volt2");
            dataGridView1.Columns.Add("", "Batt1_percentFull");
            dataGridView1.Columns.Add("", "Batt1_gotCharged");
            dataGridView1.Columns.Add("", "Batt2_percentFull");
            dataGridView1.Columns.Add("", "Batt2_gotCharged");
 
            ChangeDateFormat(dateTimePicker1);
            ChangeDateFormat(dateTimePicker2);
        }
 
        public void ChangeDateFormat(DateTimePicker dtp)
        {
            dtp.Format = DateTimePickerFormat.Custom;
            dtp.CustomFormat = "MM/dd/yyyy";
        }
 
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
 
        }
 
    }
}


and the error i receive is
system.InvalidOperationException: The ConnectionString property has not been initialized.
at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand()
at
System.Data.SqlClient.SqlConnectionFactory.permissionDemand(DbConnectionouterConnection)
at
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnectionouterConnection,DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at WindowsFormsApplication9.Form1.button1_Click(Object sender, EventArgse) in
D:\WindowsFormsApplication9\Form1.cs:line29
Posted 20-Jan-13 18:47pm
Updated 21-Jan-13 1:19am
digimanus61.5K
v5
Comments
josh-jw 21-Jan-13 1:10am
   
what error you are getting?
pranathis012 21-Jan-13 1:10am
   
What are the error's you getting please send me the error description...
Bernhard Hiller 21-Jan-13 2:44am
   
"is in mysql connection": are you sure? Your code shows a SqlConnection, i.e. Microsoft SQL Server, not MySQL!
digimanus 21-Jan-13 7:20am
   
Indeed. That is what I noted too.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You have not called Open on the connection, so it is not open. In future, give us the error message if you want us to tell you what it means.
  Permalink  
Comments
digimanus 21-Jan-13 7:20am
   
For DataAdapter I thought it was not needed to use Open() method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171019.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100