Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
void TextDateYukle()
{
  using (StreamReader sr = new StreamReader(File.Open("C:\\Tr500.txt", FileMode.Open)))
  {
    using (SqlConnection txtbaglan = new SqlConnection("server=.;database=.......;trusted_connection=true"))
    {
      txtbaglan.Open();
      string line = "";
      while ((line = sr.ReadLine()) != "")
      {
        string[] parts = line.Split(new string[] { "," }, StringSplitOptions.None);
        string cmdTxt = string.Format("INSERT INTO pdks(cihazno,cardno,shift,tarih,saat) VALUES ('{0}','{1}','{2}','{3}','{4}')", parts[0], parts[1], parts[2], parts[3], parts[4]);//", parts[0], parts[1]);
        using (SqlCommand cmddd = new SqlCommand(cmdTxt, txtbaglan))
        {
          cmddd.ExecuteNonQuery();
        }
      }
    }
  }
}



string[] parts = line.Split(new string[] { "," }, StringSplitOptions.None);

Null Reference Excaption was unhandled
Object reference not set to an instance of an object.


error in this line, Visual Studio does not give any error message or a warning, Why do you think the problem may be???

good work
Posted
Updated 5-Dec-10 21:55pm
v2
Comments
sandipapatel 6-Dec-10 4:05am    
put condition:

if(sr!=null) // execute your statements
Manfred Rudolf Bihy 6-Dec-10 4:06am    
Corrected code tag.
Manfred Rudolf Bihy 6-Dec-10 4:11am    
or on the basis on Sandipapatel' approach: while(!String.IsNullOrEmtpy(line = sr.ReadLine()))...
@Sandipapatel: error OP described is in line.Split(...) so sr probably isn't the cause. Eventhough it's always clever to check for that :)

Come on mhr!

I already answered your similar if not almost identical question here:

http://www.codeproject.com/Questions/132988/Text-file-to-SQL-import-cells-problem.aspx

I thought you'd learn something from that.

Well, the answer most probably might be this: The delimeter in your text file is not a comma as the last time. It might be semicolon or what ever.

Look into the textfile you're trying to process and you'll see.

Modification:
This is more likely the cause: Line is null since you're trying to read until line is equal to "". If there isn't an empty line in your file this may very well happen.
while(!String.IsNullOrEmtpy(line = sr.ReadLine()))
{
...
...


better check for line being either empty or null, as you'll get a null reference exception if you try to do a line.Split(...) when line is actually null.

End of modification


Crestfallen,


Manfred
 
Share this answer
 
v4
Try using this instead:
C#
while (!string.IsNullOrEmpty(line = sr.ReadLine()))
   {
   string[] parts = line.Split(',');
   ...
 
Share this answer
 
Comments
Manfred Rudolf Bihy 6-Dec-10 4:22am    
Oops, I just made a modification to my answer, that contains your solution. I promise I didn't copy you. Anyway since this was the most probable cause of what went wrong: 5 from me :)
Excellent site users and members have an excellent, thank you..

I solved the problem, but I find the current erich ...

Thank you ManfredRBihy!!!
 
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