user2931015
user2931015

Reputation: 219

Getting error of Input String Format

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

Upvotes: 0

Views: 88

Answers (2)

Vaibhav Parmar
Vaibhav Parmar

Reputation: 643

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));

Upvotes: 1

The Hungry Dictator
The Hungry Dictator

Reputation: 3484

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

bind like this in your aspx page....

Upvotes: 1

Related Questions