locked
operator += cannot be applied with string and void asp.net RRS feed

  • Question

  • User-367318540 posted

    following error is coming

    operator += cannot be applied with string and void asp.net

    on this below line

     

     ResultValue +=Convert.ToString(this.InsertRows(s_No, v_Name, rate, s_liter, entryDate);
    below is my whole code
    string s_No, v_Name, rate, s_liter, entryDate, ResultValue = Convert.ToString(0); 
    
                    foreach (DataRow row in dt.Rows)
                    {
    
    
                        s_No = (row["S_No"].ToString());
                        v_Name = (row["V_Name"].ToString());
                        rate = (row["Rate"].ToString());
                        s_liter = (row["S_liter"].ToString());
                        entryDate = (row["EntryDate"].ToString());
                        ResultValue +=Convert.ToString(this.InsertRows(s_No, v_Name, rate, s_liter, entryDate);
                    }
                    if (ResultValue == dt.Rows.Count)
                    {
                        // Response.Redirect("BalePack.aspx");
                        Response.Redirect("Packrpt.aspx?PID=" + I_ID + "");
                    }
                    else
                    {
                        Response.Write("<script>alert('Not Save'</script>");
                    }
                }
            }
            
            private void InsertRows(string s_No, string v_Name, string rate, string s_liter, string entryDate)
            {
                //con.Open();
                using (SqlCommand cmd = new SqlCommand("Sp_InsertInvM", con))
                {
                    cmd.Parameters.AddWithValue("@Action", "INSERTD");
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@I_ID", I_ID);
                    cmd.Parameters.AddWithValue("@S_Date", entryDate);
                    cmd.Parameters.AddWithValue("@S_No", s_No);
                    cmd.Parameters.AddWithValue("@P_Price", rate);
                    cmd.Parameters.AddWithValue("@S_Liter", s_liter);
    
                    con.Open();
                    cmd.ExecuteNonQuery();
    
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('Duplicate Order Save SuccessFully.'); window.open('SOgvud.aspx');", true);
    
                    con.Close();
    Tuesday, April 14, 2020 8:08 AM

Answers

  • User415553908 posted

    Convert.ToString() needs an object to work with. The function you use as its argument does not return anything at all: private void InsertRows(...), resulting in the exception you see.

    Looking at your code, it seems you are trying to verify how many rows your stored proc processed. You could modify it to look something along these lines:

    string s_No, v_Name, rate, s_liter, entryDate;
    int ResultValue = 0;
    
    foreach (DataRow row in dt.Rows)
    {
    	s_No = (row["S_No"].ToString());
    	v_Name = (row["V_Name"].ToString());
    	rate = (row["Rate"].ToString());
    	s_liter = (row["S_liter"].ToString());
    	entryDate = (row["EntryDate"].ToString());
    	ResultValue += this.InsertRows(s_No, v_Name, rate, s_liter, entryDate); // no conversion needed 
    }
    if (ResultValue == dt.Rows.Count)
    {
    	// Response.Redirect("BalePack.aspx");
    	Response.Redirect("Packrpt.aspx?PID=" + I_ID + "");
    }
    else
    {
    	Response.Write("<script>alert('Not Save'</script>");
    }
                
    private int InsertRows(string s_No, string v_Name, string rate, string s_liter, string entryDate)
    {
    	//con.Open();
    	using (SqlCommand cmd = new SqlCommand("Sp_InsertInvM", con))
    	{
    		cmd.Parameters.AddWithValue("@Action", "INSERTD");
    		cmd.CommandType = CommandType.StoredProcedure;
    		cmd.Parameters.AddWithValue("@I_ID", I_ID);
    		cmd.Parameters.AddWithValue("@S_Date", entryDate);
    		cmd.Parameters.AddWithValue("@S_No", s_No);
    		cmd.Parameters.AddWithValue("@P_Price", rate);
    		cmd.Parameters.AddWithValue("@S_Liter", s_liter);
    
    		con.Open();
    		int result = cmd.ExecuteNonQuery(); // get the value from your stored proc (I assume you want first value of the first result set it returns as per documentation: https://docs.microsoft.com/en-us/dotnet/api/microsoft.data.sqlclient.sqlcommand.executenonquery?view=sqlclient-dotnet-1.1)
    
    		ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('Duplicate Order Save SuccessFully.'); window.open('SOgvud.aspx');", true);
    
    		con.Close();
    		return result; // return a value
    }

    Performance suggestion: since you concatenate string in a foreach loop, you might want to consider using StringBuilder instead

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 14, 2020 8:33 AM

All replies

  • User753101303 posted

    Hi,

    InsertRows doesn"t return any value (void). It seems you want to use 

    ResultValue +=this.InsertRows(s_No, v_Name, rate, s_liter, entryDate);

    And have InsertRows to return the value returned by cmd.ExecuteNonQuery ?

    Tuesday, April 14, 2020 8:21 AM
  • User415553908 posted

    Convert.ToString() needs an object to work with. The function you use as its argument does not return anything at all: private void InsertRows(...), resulting in the exception you see.

    Looking at your code, it seems you are trying to verify how many rows your stored proc processed. You could modify it to look something along these lines:

    string s_No, v_Name, rate, s_liter, entryDate;
    int ResultValue = 0;
    
    foreach (DataRow row in dt.Rows)
    {
    	s_No = (row["S_No"].ToString());
    	v_Name = (row["V_Name"].ToString());
    	rate = (row["Rate"].ToString());
    	s_liter = (row["S_liter"].ToString());
    	entryDate = (row["EntryDate"].ToString());
    	ResultValue += this.InsertRows(s_No, v_Name, rate, s_liter, entryDate); // no conversion needed 
    }
    if (ResultValue == dt.Rows.Count)
    {
    	// Response.Redirect("BalePack.aspx");
    	Response.Redirect("Packrpt.aspx?PID=" + I_ID + "");
    }
    else
    {
    	Response.Write("<script>alert('Not Save'</script>");
    }
                
    private int InsertRows(string s_No, string v_Name, string rate, string s_liter, string entryDate)
    {
    	//con.Open();
    	using (SqlCommand cmd = new SqlCommand("Sp_InsertInvM", con))
    	{
    		cmd.Parameters.AddWithValue("@Action", "INSERTD");
    		cmd.CommandType = CommandType.StoredProcedure;
    		cmd.Parameters.AddWithValue("@I_ID", I_ID);
    		cmd.Parameters.AddWithValue("@S_Date", entryDate);
    		cmd.Parameters.AddWithValue("@S_No", s_No);
    		cmd.Parameters.AddWithValue("@P_Price", rate);
    		cmd.Parameters.AddWithValue("@S_Liter", s_liter);
    
    		con.Open();
    		int result = cmd.ExecuteNonQuery(); // get the value from your stored proc (I assume you want first value of the first result set it returns as per documentation: https://docs.microsoft.com/en-us/dotnet/api/microsoft.data.sqlclient.sqlcommand.executenonquery?view=sqlclient-dotnet-1.1)
    
    		ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('Duplicate Order Save SuccessFully.'); window.open('SOgvud.aspx');", true);
    
    		con.Close();
    		return result; // return a value
    }

    Performance suggestion: since you concatenate string in a foreach loop, you might want to consider using StringBuilder instead

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 14, 2020 8:33 AM