Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I have a window opened on a button click. When i try to close that window (On clicking the cross mark), i'm getting an exception stating "Unhandled exception has occured in your aplication. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.

String cannot be of Zero length.
Parameter name: old Value

Thanks in advance.
Posted
Comments
The Doer 7-Jan-16 2:46am    
Post your source code here.
Member 8010354 7-Jan-16 2:57am    
Form2 f = new Form2();
f.cmbColumnCombo.DataSource = cmbList;
f.ShowDialog();

for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Contains(f.txtfind.Text.ToLower()))
{
//dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Replace(f.txtfind.Text.ToLower(), f.txtreplace.Text);
//bulidDataRow(i);
dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
bulidDataRow(i);
}
}

This is probably occurring because your code is not checking to see how Form f was closed.

Try this instead:

C#
Form2 f = new Form2();
f.cmbColumnCombo.DataSource = cmbList;

//Notice the change here.
//We are checking to see how the form was closed.
if (f.ShowDialog() == DialogResult.OK) {
   for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
   {
      if (dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Contains(f.txtfind.Text.ToLower()))
      {
         //dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Replace(f.txtfind.Text.ToLower(), f.txtreplace.Text);
         //bulidDataRow(i);
         dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
         bulidDataRow(i);
      }
   }
}
else {
   //Form was quit without any action
   MessageBox.Show("No changes were made.");
}


To ensure that this works, make sure that the "OK" button (or equivalent) on Form2 has its DialogResult property set to DialogResult.OK.

Let me know how you go!
 
Share this answer
 
v3
Comments
Member 8010354 7-Jan-16 4:12am    
I have used the else statement and set the dialogue property to OK but still i face the same issue.
Mitchell J. 7-Jan-16 5:03am    
Righto. On which line is the error being thrown?
VB.NET
Try this.
This code open window fromclient side and redirect by new url in same open window, if any exception occures then close window after 4 sec.

code:

Protected Sub EditSchematic_click(ByVal sender As Object, ByVal e As EventArgs) Handles EditSchematic.Click
        Try
            
            Dim URL As String = "Google.com"
            ScriptManager.RegisterStartupScript(Me, Me.GetType, "myUniqueKey", "OpenWebWindow('" & URL & "'," & ConfigurationManager.AppSettings("WindowWidth") & "," & ConfigurationManager.AppSettings("WindowHeight") & ");", True)
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Me, Me.GetType, "myUniqueKey", "CloseWindow()", True)
        End Try

    End Sub

aspx code:

 <asp:Button CssClass="btn" runat="server" ID="EditSchematic" OnClientClick="openblankwindow();" />

<script>
function openblankwindow() {
                width = screen.width;
                height = 0.87 * screen.height;
                var st = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,left=" + 0 + ",top=" + 0 + ",width=" + width + ",height=" + height;
                win = window.open("Processing.htm", 'WindowName', st);
            }
function CloseWindow() {
            width = screen.width;
            height = 0.87 * screen.height;
            var st = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,left=" + 0 + ",top=" + 0 + ",width=" + width + ",height=" + height;
            win = window.open("errorMsgInOpenning.htm", 'WindowName', st);
            setTimeout(function () {
                win.close()
            }, 4000);
        }
function OpenWebWindow(URL, width, height) {
            //        screen.availWidth, screen.availHeight
            var left = 0;
            var top = 0;
            if (width <= 0) {
                width = screen.width;
            }
            else {
                left = (screen.width / 2) - (width / 2);
            }
            if (height <= 0) {
                height = 0.87 * screen.height;
            }
            else {
                top = (screen.height / 2) - (height / 2);
            }

            var st = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height;
            window.open(URL, 'WindowName', st);
           
        }
</script>
 
Share this answer
 
Comments
Member 8010354 7-Jan-16 4:38am    
Don't mind. I didn't understand the code.
Member 8010354 7-Jan-16 5:01am    
I got it by using this code
if (!string.IsNullOrEmpty(f.txtfind.Text))
{
dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);

Thanks.

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