-2

Below is my code. Where I am passing connection string in Openconnection() function. But when Openconnection method is invoked, I am getting error.

 public class dbconn
    {
        string ConnetionString = null;
        string State = "";
        SqlConnection cnn;
    public string openconnection()
    {
        ConnetionString = @"Data Source=myservername;Initial Catalog=mydbname;Integrated Security=True";
        cnn = new SqlConnection(ConnetionString);
        try
        {
            cnn.Open();
            return State;
        }
        catch (Exception ex)
        {
             ...............
        }
    }

    public string closeconnection()
    {
        ConnetionString = @"Data Source=myservername;Initial Catalog=mydbname;Integrated Security=True";
        cnn = new SqlConnection(ConnetionString);
        try
        {
            cnn.Close();
            return State;
        }
        catch (Exception ex)
        {
           .........
        }
    }



}

when I am trying to call this class from my c# code part like

public dbconn dbcon;
private void btnsave_Click(object sender, EventArgs e)
{
  string State="";
  try
  {
    State= dbcon.openconnection();//*** Error in this line
    if (State!= "")
    {
      MessageBox.Show(State, "Lost Connection");
    }
 }
}

In *** part I've got error.

ali durgun
  • 25
  • 4

1 Answers1

2

From your comment, I was right. You need to instantiate your dbconn object.

dbcon = new dbconn();

Overall though, I'd advise against using this sort of practice for SQL connections. Please look into using for whenever you're doing any sort of database access. (As Tim Schmelter has already stated in the comments)

FakeCaleb
  • 982
  • 8
  • 19
  • I didn't get this error this time but my connection didn't opened – ali durgun Dec 21 '16 at 15:58
  • I can't help from 'didn't opened'. What error are you getting? What exactly is happening? – FakeCaleb Dec 21 '16 at 16:00
  • I don't get any error my program trying to open and it's going to catch then – ali durgun Dec 21 '16 at 16:01
  • I recommend printing the exception. `System.Diagnostics.Debug.WriteLine(ex.message);` in your catch and read whats happening, that will massively help you – FakeCaleb Dec 21 '16 at 16:02
  • I have ProcDurum = urunekle.proc(int.Parse(cbmüşteriseç.ToString()),txtürünadı.‌​ToString(),dtcürüngi‌​riştarihi.ToString()‌​,float.Parse(txtkilo‌​.ToString()), int.Parse(txtürünadeti.ToString()),txtkasatipi.ToString(),tx‌​türünözelliği.ToStri‌​ng(),txtplaka.ToStri‌​ng()); in my code line and I have got catch expection in there – ali durgun Dec 21 '16 at 16:06
  • public urunekleproc urunekle=new urunekleproc(); i have that code line also – ali durgun Dec 21 '16 at 16:07
  • I think I understand what is wrong I have stored procedure and when I am trying to add record with using storeprocedureclass I have got error. @FakeCaleb – ali durgun Dec 21 '16 at 16:09
  • It seems that way (though due to the formatting and language, I'm struggling to comprehend the exact issue), as none of what you showed us above relates to what you are showing me here. I'm glad you figured out you're issue though. – FakeCaleb Dec 21 '16 at 16:11
  • 1
    Thanks again for your help :) – ali durgun Dec 21 '16 at 16:15