ExecuteReader: Connection property has not been initialized. Browser Game [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We are having some problems displaying the experience a player has from our database.
This is for a school project, would like some hints :)
SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=#uid");
string uID = Session["userID"].ToString();
CheckExp.Parameters.AddWithValue("#uid", uID);
try
{
connection.Open();
SqlDataReader ExpReader = null;
ExpReader = CheckExp.ExecuteReader();
if (ExpReader.Read())
{
Label6.Text = ExpReader["Experience"].ToString();
}
}
catch (Exception ex)
{
Label6.Text=(ex.Message);
}
finally
{
connection.Close();
}

You didn't connect your SqlConnection and SqlCommand.
Just define your connection as a second parameter like;
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=#uid", connection);
Or you can assing your SqlCommand.Connection property like;
CheckExp.Connection = connection;

Related

How to access Sql server online database with my C# windows application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have windows application using C#, i want to store the data in local SQL SERVER database and as well as online SQL SERVER Database.
You shouldn't be asking questions like this here, but here's something to get you started.
using(SqlConnection cnn = new SqlConnection())
{
cnn.ConnectionString="your connection string";
// /*e.g.: */
cnn.ConnectionString = "Server=123.123.123.123,1433;Database=DbName;User id=user;Password=pass;";
using(SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure; //or whatever it is
cmd.ConnectionTimeout = 0; //or however long you want
cmd.Connection = cnn;
cmd.CommandText = "your query";
//cmd.Parameters.AddWithValue("#SP_PARAM", object); if you are calling an SP
cnn.Open();
cmd.ExecuteNonQuery(); //whatever you are executing
cnn.Close();
}
}

Login Form using sql database c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make a sign up page . you put user name and password and its save in the sql database.
note: evrey thing worked until i add the 2nd column(the password).
this is the password code(the username is the same) :
static public void delete(string _Password)
{
try
{
connection.Open();
SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table] VALUES(#Password)", connection);
commandInsert.Parameters.Add("Password", _Password);
commandInsert.ExecuteNonQuery();
}
catch (SqlCeException expection)
{
MessageBox.Show(expection.ToString());
}
finally
{
connection.Close();
}
}
And this is the button settings :
private void button1_Click(object sender, EventArgs e)
{
if (deleteBox.Text != "")
{
SQLFunctions.Insert(insertBox.Text);
SQLFunctions.delete(deleteBox.Text);
SQLFunctions.Refresh(this.dataGridView1);
}
else
{
MessageBox.Show("login failed");
}
}
thanks
I guess you need to modify your code little bit like this
SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table] VALUES(#username,#Password)", connection);
you should pass two parameters as you have two fields in db now and if you don't want to pass username then you should specify this thing like this
SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table](password) VALUES(#Password)", connection);
and also
commandInsert.Parameters.Add("Password", _Password);
this should be written as follows
commandInsert.Parameters.Add("#Password", _Password);
and to pass values to this parameter, you can do it like this
command.Parameters.AddWithValue("#Password", insertBox.Text);

what is the query of sql database connection [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to connect my C# project with SQL database.
which query I have to follow??
using(SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
}
i use this, it is not working
Try this
namespace Town
{
class clsconnection
{
string connection = #"Data Source=server name;Initial Catalog=databasename;Integrated Security=True";
public SqlConnection getconnection()
{
SqlConnection sql = new SqlConnection(connection);
sql.Open();
return sql;
}
}
}

Insert record into Access 2000 database in C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I need to insert a record in Access 2000 database using C#. The code fails at the SqlConnection. Please help.
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Trading.mdb";
string commandText = "INSERT INTO Order (OpenDate) VALUES (#OpenDate)";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.AddWithValue("#OpenDate", DateTime.Now);
try
{
command.Connection.Open();
int response = command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error: {0}" + ex.Message);
}
}
Problem : you are working with MS-Access Database but using SqlServer objects.
Solution : you need to use OleDbConnection object instead of SqlConnection and OleDbCommand instead of SqlCommand
Try This:
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Trading.mdb";
string commandText = "INSERT INTO Order (OpenDate) VALUES (?)";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(commandText, connection);
command.Parameters.AddWithValue("#OpenDate", DateTime.Now);
try
{
command.Connection.Open();
int response = command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error: {0}" + ex.Message);
}
}
You need OleDbConnection not SqlConnection which is used for SQL Server.
See: Walkthrough: Editing an Access Database with ADO.NET
also: How to: Create Connections to Access Databases

connect to oracle XE using c# 2010 express [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Just startted with c#. not sure the express version supports connection to oracle or not.
if it does, could anyone let me know the steps to do it?
Thanks guys.
Use the ODP .NET ADO .NET Provider. You can see an example of how to use it here.
Copied from the example
using System;
using System.Data;
using Oracle.DataAccess.Client;
...
// Create the connection object
OracleConnection con = new OracleConnection();
// Specify the connect string
// NOTE: Modify User Id, Password, Data Source as per your database set up
con.ConnectionString = "User Id=userid;Password=password;Data Source=dbinstance;";
try
{
// Open the connection
con.Open();
Console.WriteLine("Connection to Oracle database established!");
Console.WriteLine(" ");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
string cmdQuery = "SELECT empno, ename FROM emptab";
// Create the OracleCommand object
OracleCommand cmd = new OracleCommand(cmdQuery);
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
try
{
// Execute command, create OracleDataReader object
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
// Output Employee Name and Number
Console.WriteLine("Employee Number: " +
reader.GetDecimal(0) +
" , " +
"Employee Name : " +
reader.GetString(1));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Dispose OracleCommand object
cmd.Dispose();
// Close and Dispose OracleConnection object
con.Close();
con.Dispose();
}

Categories

Resources