Click here to Skip to main content
16,016,712 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
System.InvalidOperationException
HResult=0x80131509
Message=Fill: SelectCommand.Connection property has not been initialized.
Source=<cannot evaluate="" the="" exception="" source="">
StackTrace:
<cannot evaluate="" the="" exception="" stack="" trace="">




using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace project
{
public class DAL
{
private SqlConnection connection;
private SqlCommand command;
private SqlDataAdapter adapter;
private string SQLstr;
private string connectionString;

public DAL()
{
connectionString = string.Format(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\zafal\source\repos\finalprojectrecipe\finalprojectrecipe\db.mdf;Integrated Security=True;Connect Timeout=30");
SQLstr = "Select *from Users";
command = new SqlCommand(SQLstr, connection);
adapter = new SqlDataAdapter(command);
}

public DataSet GetDataSetWithSql(string strSQL)
{
DataSet dataset = new DataSet();
command.CommandText = strSQL;
adapter.SelectCommand = command;
adapter.SelectCommand = command;

adapter.Fill(dataset);
return dataset;
}

public int insert(string fname, string lname, string email, string password)
{
int success = -1;

using(SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "INSERT INTO Users(fname,lname,EMAIL,password,is admin) VALUES(@pass,@email,@param3,@param4,@param5,@param6)";
using (SqlCommand cmd = new SqlCommand(sql, connection))
{
cmd.Parameters.Add("@email", SqlDbType.NVarChar, 40).Value = email;
cmd.Parameters.Add("@pass", SqlDbType.NVarChar, 40).Value = password;
cmd.Parameters.Add("@param3", SqlDbType.NVarChar, 20).Value = fname;
cmd.Parameters.Add("@param4", SqlDbType.NVarChar, 20).Value = lname;
cmd.Parameters.Add("@param5", SqlDbType.Int).Value = 0;
cmd.Parameters.Add("@param6", SqlDbType.TinyInt).Value = 0;
cmd.CommandType = CommandType.Text;
success = cmd.ExecuteNonQuery();

What I have tried:

i have tried chnaging the code but im a newbie and im not sure what to do
Posted
Updated 6-Apr-20 0:04am

Read the error message:
Message=Fill: SelectCommand.Connection property has not been initialized.

It can't be much clearer than that!
So now look at your code that uses the Fill method: there is only one call to Fill in that code:
C#
public DataSet GetDataSetWithSql(string strSQL)
    {
    DataSet dataset = new DataSet();
    command.CommandText = strSQL;
    adapter.SelectCommand = command;
    adapter.SelectCommand = command;
    
    adapter.Fill(dataset);
    return dataset;
    }
Where in that code is the Connection set?
No Connection == no idea where to get the data from == error message.
 
Share this answer
 
Comments
alexzaf 5-Apr-20 17:49pm    
what is a connection set?
OriginalGriff 6-Apr-20 2:08am    
"Where in that code is the Connection set?"
Let me try to rephrase that ...
"Look at your code.
Find everywhere you use the Connection object.
Of all those places, which one gives it a value instead of reading a value from it?"
In
public DAL()
{
after assigning
connectionString 

add this code:
connection = new SqlConnection(connectionString);

It should work. Although your code has many other code refactor requirements.
But as of now What I suggested should solve your issue.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900