Community discussion forum

Identifying the failed records???

  • 1 year ago
    hai,
    here i am updating 100 records at the same time.
    when the update fails, the record is to store in a log file (.txt).
    how to implement.?
     
    FOR EXAMPLE............. 
     
    if i m updating rollno 100,200,300,400,500 (via. ASP.NET with C#  to  SQL Server 2000)  and due to some reasons the 100,200 fails to update,... then those 100 and 200 should store in some text file or EXCEL file, so that the user may know which records are failed to update in database.......

    i m using ASP.NET with C# (.net 2005, 2.0 framework)
  • 1 year ago

    You were not exactly clear on many details, so don't expect a very clear reply.

    I imagine you're calling a SQL stored procedure to perform the update(s) to your database.

    What I don't know is what your definition of "Update fails" means. Would this fail because of some exception that gets thrown? If so, use a try/catch block.

    Would it fail because of some condition not being met? In that case you can either throw your own exception (from the stored procedure) or you can set a specific return value that tells your code what happened, or you can set an output parameter to communicate the status of the stored procedure call back to your C# code. This is a good article for teaching you the various ways or returning info from a stored procedure: http://aspnet.4guysfromrolla.com/articles/062905-1.aspx

    Once you know there's a problem I imagine you can figure out how to write the text to a file. (http://aspnet.4guysfromrolla.com/articles/072303-1.aspx) However, there are many possible problems in that scenario as well. What happens when it fails to write to your text file?

     

  • 1 year ago

    for (i=1;i=5;i++)
    {
    int a=i;
    sqlinsert="update tbl_name set loop_no=a where rollnumber=a";
    cmd = new SqlCommand(sqlinsert, conn);
    try
    {
    cmd.ExecuteNonQuery();
    }
    catch(Exception ex)
    {
    FileStream fs;
    StreamWriter sw;
    fs=new Filestream("logfile.txt",FileMode.open);
    sw=new StreamWriter(fs);
    string newline="Roll No:"+a+"was not updated";
    sw.close();
    fs.close();
    }

    }

    once if its go for "CATCH" block, how it can proceed others?
    i.e, if record 2 fails, will it proceed other records and complete the whole FOR loop????
    i tried.. but getting error...
    how to achieve it?
    thanks in advance and understanding....

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!