0

i use a repeater instead of grid view .In button i done with code but when i debug it shows me error....and i am trying approving documents but error occur

ok now i posted the whole code,,

   protected void Button1_Click(object sender, EventArgs e)
    {
     string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
          SqlConnection mySQLconnection = new SqlConnection(connStr);
          if (mySQLconnection.State == ConnectionState.Closed)
          {
              mySQLconnection.Open();

              for (int i = 0; i < Repeater2.Items.Count; i++)
              {
                  DropDownList DropDownListcontrol =
              ((DropDownList)Repeater2.Items[i].FindControl("DropDownList4"));
                  Label DocId = ((Label)Repeater2.Items[i].FindControl("DocId"));
                 SqlCommand cmd = new SqlCommand("approveddd", mySQLconnection);
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.Parameters.Add("@DocID", SqlDbType.Int).Value =
                 Convert.ToInt32((DocId.Text));

                  cmd.Parameters.Add("@ApproveID", SqlDbType.Int).Value = 
            Convert.ToInt32(DropDownListcontrol.SelectedValue);
                  cmd.Parameters.Add("@ApproveBy", SqlDbType.VarChar, 50).Value = 
               (Session["Login2"]);

                  cmd.ExecuteNonQuery();
                  //UPDATE APPPROVEID IN DOCUMENTINFO TABLE
                  DMSLIB.Doc myDoc = new DMSLIB.Doc();
                  myDoc.MarkDocAs(Convert.ToInt16(DocId.Text), 
                Convert.ToInt32(DropDownListcontrol.SelectedValue));

              }

          }
          else
          {
              apfi.Text = "Error";
          }
          if (mySQLconnection.State == ConnectionState.Open)
          {
              mySQLconnection.Close();
          }
                }

error in this line

                      cmd.Parameters.Add("@DocID", SqlDbType.Int).Value =
             Convert.ToInt32((DocId.Text));

any idea?

ERROR: INPUT STRING WAS NOT IN CORRECT FORMAT

2
  • Does DocId.Textcontains a number? An empty value, for instance, will generate an error. Commented Nov 14, 2013 at 11:54
  • in html like this..... <td> <asp:Label Id="DocId" runat="server"></asp:Label> <%#DataBinder.Eval(Container.DataItem, "DocID")%> </td> Commented Nov 14, 2013 at 11:54

2 Answers 2

1

That error comes because if DocId.Text is empty, you are trying to convert an empty string into an integer, and this is what gives you the "Input string was not in a correct format." exception.

You need to first of all detect if the DocID is an empty string, and assign -1 (say) to your cmd instance.

you have to first do:

DocId.Text= (DocId.Text == "") ? DBNull.Value : Convert.ToInt32(DocId.Text);

then assign it to cmd object

cmd.Parameters.Add("@DocID", SqlDbType.Int).Value =Convert.ToInt32((DocId.Text));
Sign up to request clarification or add additional context in comments.

Comments

1
<td> <asp:Label Id="DocId" text='<%#DataBinder.Eval(Container.DataItem, "DocID")%>' runat="server"></asp:Label>  </td>

bind like this in your aspx page....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.