4
Answers

object reference not set to an instance of object c#

ahmed salah

ahmed salah

7y
1.5k
1
I work in windows form c# vs 2015 i get error i need to solve it
 
but i dont know how to solve it
 
i write function in textbox changed event but when i start writing in textbox
 
get error object reference not set to an instance of object .
 
so that why this problem happen and how to solve it .
 
in textbox changed event
 
  1. private void textBox3_TextChanged(object sender, EventArgs e)  
  2.        {  
  3.            try  
  4.            {  
  5.                 
  6.   
  7.                    MatrixClass matrix = new MatrixClass();  
  8.                    string value1 = matrix.GetAtcCode(textBox3.Text);  
  9.                    textBox1.Text = value1;  
  10.                     
  11.                  
  12.            }  
  13.            catch (Exception ex)  
  14.            {  
  15.                MessageBox.Show(ex.Message);  
  16.            }  
  17.        }  
 
  1. public string GetAtcCode(string GenericName)  
  2.        {  
  3.            SqlConnection con = new SqlConnection(connection);  
  4.             
  5.            con.Open();  
  6.   
  7.            SqlCommand comm = new SqlCommand("ATC_Code_ByGeneric_H", con);  
  8.   
  9.            comm.CommandType = CommandType.StoredProcedure;  
  10.   
  11.            comm.Parameters.Add(new SqlParameter("@Lvl_Name", GenericName));  
  12.   
  13.            string ret = comm.ExecuteScalar().ToString();   
  14.            con.Close();  
  15.   
  16.            return ret;  
  17.   
  18.        }  
 stored procedure 
  1. ALTER PROCEDURE [dbo].[ATC_Code_ByGeneric_H]  
  2.     @Lvl_Name nvarchar(500)  
  3. AS  
  4.     SELECT     Tbl_Lvl1.Code + Tbl_Lvl2.Code + Tbl_Lvl3.Code + Tbl_Lvl4.Code + Tbl_Lvl5.Code AS ATC_Code  
  5.     FROM         Tbl_Lvl1  
  6.                  INNER JOIN  
  7.                  Tbl_Lvl2  
  8.                  ON Tbl_Lvl1.S_ID = Tbl_Lvl2.UpLvl_ID  
  9.                  INNER JOIN  
  10.                  Tbl_Lvl3  
  11.                  ON Tbl_Lvl2.S_ID = Tbl_Lvl3.UpLvl_ID  
  12.                  INNER JOIN  
  13.                  Tbl_Lvl4  
  14.                  ON Tbl_Lvl3.S_ID = Tbl_Lvl4.UpLvl_ID  
  15.                  INNER JOIN  
  16.                  Tbl_Lvl5  
  17.                  ON Tbl_Lvl4.S_ID = Tbl_Lvl5.UpLvl_ID  
  18.     WHERE     (Tbl_Lvl5.Lvl_Name = @Lvl_Name) and Tbl_Lvl5.human=1  
  19.     RETURN  
 
Answers (4)