-1

I have the following code:

OpeningForm of = new OpeningForm();
of.ShowDialog();

SqlConnection SqlBaglanti = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=MolaTakip; integrated security=true");
SqlBaglanti.Open();

SqlCommand komut = new SqlCommand("AgentBilgisiDondur')", SqlBaglanti);
komut.CommandType = System.Data.CommandType.StoredProcedure;
komut.Parameters.AddWithValue("@LoginName", ThisAgent.LoginName);

SqlDataReader dr = komut.ExecuteReader();

while (dr.Read())
{
    ThisAgent.AgentID = int.Parse(dr["UserID"].ToString());
    ThisAgent.AgentAdi = dr["UserName"].ToString();
}
SqlBaglanti.Close();

FileStream fs = new FileStream(@"C:\agent.bin", FileMode.OpenOrCreate);

BinaryFormatter bf = new BinaryFormatter();

bf.Serialize(fs, ThisAgent);

All of this codes written in the Form_Load event. But form loads at

SqlDataReader dr = komut.ExecuteReader();

line, further commands are not running.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sefa
  • 8,865
  • 10
  • 51
  • 82
  • 2
    Are you getting an exception? Also your code is badly leaking handles. You are not disposing any of the IDisposable resources you are dealing with. – Darin Dimitrov Aug 18 '12 at 11:29
  • Run your program in the debugger, and see if an exception pops up? – Andomar Aug 18 '12 at 11:30
  • No exceptions, i also followed my code with break points. I'm %100 sure about it's not going further from that line. – Sefa Aug 18 '12 at 11:36
  • Add a try...catch around all code and get the exception(s). If there are no exceptions, the resultset might be empty. – Emond Aug 18 '12 at 11:42
  • You should post your sql and exception you got – cuongle Aug 18 '12 at 11:52

3 Answers3

2

Are you sure about the syntax?

SqlCommand komut = new SqlCommand("AgentBilgisiDondur')", SqlBaglanti);

I think you have a stored procedure which is called AgentBilgisiDondur not AgentBilgisiDondur')

SqlCommand komut = new SqlCommand("AgentBilgisiDondur", SqlBaglanti);

An advice: Also try to use English because your columns are in English such as UserName. So you can try ReturnAgentInformation ;)

Stack User
  • 1,378
  • 5
  • 19
  • 40
0

You must open the database connection using the sql command and not the sqlconnection itself. You rightly passed t to the command. Now the command must open it.

Remove SqlBaglanti.Open(); before you call the ExecuteReader() add this line:

SqlCommand.Connection.Open();

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
0

the mistake is that you have not correct parameters (are you missed some parameters like passowrd agent for your stored procedure or the name of stored procedure is wrong) check please

Hassan Boutougha
  • 3,871
  • 1
  • 17
  • 17