Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am trying to convert a Label Text string into Integer datatype and store it in SQL Database. I had it implemented and running in my program just fine before this morning. But then I had to copy paste whole project from old .cs to new .cs, and now it is throwing error.

My code is:

C#
protected void btnUpdate_Click(object sender, EventArgs e)
{
    MultiView1.SetActiveView(vGrid);

    using (SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True"))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            string sql = "UPDATE dbo.Documents SET Ref = @Ref, Subject = @Subject, Src = @Src, Dst = @Dst, Medium = @Medium, Date_Printed = @Date_Printed, Date_Received = @Date_Received, Document_Type = @Document_Type,Action_Required = @Action_Required, Due_Date = @Due_Date, Actual_Date = @Actual_Date, Content = @Content, Tag = @Tag, Issue_No = @Issue_No, Attachment = @Attachment, Notes = @Notes, Assigned_To = @Assigned_to, Reply_Ref = @Reply_Ref, Priority = @Priority, Status = @Status, Response = @Response  WHERE DocumentsID = @DocumentsID ";

            cmd.Connection = con;
            cmd.CommandText = sql;

            cmd.Parameters.Add(new SqlParameter("@Ref", txtRef.Text));
            cmd.Parameters.Add(new SqlParameter("@Subject", txtSubject.Text));
            cmd.Parameters.Add(new SqlParameter("@Src", ddlSource.Text));
            cmd.Parameters.Add(new SqlParameter("@Dst", ddlDestination.Text));
            cmd.Parameters.Add(new SqlParameter("@Medium", ddlMedium.Text));
            cmd.Parameters.Add(new SqlParameter("@Date_Printed", txtDatePrinted.Text == "" ? DBNull.Value : (object)txtDatePrinted.Text));
            cmd.Parameters.Add(new SqlParameter("@Date_Received", txtDateReceived.Text == "" ? DBNull.Value : (object)txtDateReceived.Text));
            cmd.Parameters.Add(new SqlParameter("@Document_Type", ddlDocumentType.Text));
            cmd.Parameters.Add(new SqlParameter("@Action_Required", cbxAction.Checked));
            cmd.Parameters.Add(new SqlParameter("@Due_Date", txtDueDate.Text == "" ? DBNull.Value : (object)txtDueDate.Text));
            cmd.Parameters.Add(new SqlParameter("@Actual_Date", txtActualDate.Text == "" ? DBNull.Value : (object)txtActualDate.Text));
            cmd.Parameters.Add(new SqlParameter("@Content", txtContent.Text));
            cmd.Parameters.Add(new SqlParameter("@Tag", txtTag.Text));
            cmd.Parameters.Add(new SqlParameter("@Issue_No", txtIssue.Text));
            cmd.Parameters.Add(new SqlParameter("@Attachment", txtAttachment.Text));
            cmd.Parameters.Add(new SqlParameter("@Notes", txtNotes.Text));
            cmd.Parameters.Add(new SqlParameter("@Assigned_To", ddlAssignedTo.Text));
            cmd.Parameters.Add(new SqlParameter("@Reply_Ref", txtReplyRef.Text));
            cmd.Parameters.Add(new SqlParameter("@Priority", ddlPriority.Text));
            cmd.Parameters.Add(new SqlParameter("@Status", ddlStatus.Text));
            cmd.Parameters.Add(new SqlParameter("@Response", ddlResponse.Text));
            cmd.Parameters.Add(new SqlParameter("@DocumentsID", Int32.Parse(lblSet.Text)));

            // cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            //dataset object to get all select statement results
            //DataSet ds = new DataSet();

            //sql dataadoptor to fill dataset
            cmd.ExecuteNonQuery();
        }
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
    }
}



It is throwing an error on the following Line:
C#
cmd.Parameters.Add(new SqlParameter("@DocumentsID", Int32.Parse(lblSet.Text)));

Error Message:
Input string was not in a correct format.

Any help would be much appreciated.

Kind regards.
Posted
Updated 25-Jun-13 21:23pm
v3
Comments
Thomas Daniels 26-Jun-13 3:24am    
What's the value of <small>lblSet.Text</small>?
[no name] 26-Jun-13 3:35am    
in the debug mode, lblSet = {Text: "..."}

I have assigned this value when i defined lblSet on the webform.
Thomas Daniels 26-Jun-13 3:37am    
Then you get the error because you can't convert "..." to an integer. Put a valid string representation of an integer in the Text property.

Yes, this error is obvious. You cannot convert a string which does not represents a 32-bit signed integer . You can use Int32.TryParse Method[^].
Try this:
C#
int value;
bool result = Int32.TryParse(lblSet.Text.Trim(), out value);
//If converted
if(result)
{
    //Add the parameter
    cmd.Parameters.Add(new SqlParameter("@DocumentsID", value));
}



--Amit
 
Share this answer
 
You can have a look at below link
convert label text to int[^]
 
Share this answer
 
It is throwing an error on the following Line:
C#
cmd.Parameters.Add(new SqlParameter("@DocumentsID", Int32.Parse(lblSet.Text)));


Date in your [lblSet.Text] is not numbers aur greater then Int32 max value. make sure your are inserting correct Int32 integer and there is no space are any extra character in your [lblSet.Text]
or use TryParse.

C#
int customer_ID=0;
int.TryParse(lblSet.Text, out customer_ID) ? customer_ID : -1;
 
Share this answer
 
v3
use
VB
cmd.Parameters.Add(new SqlParameter("@DocumentsID", Convert.ToInt16(lblSet.Text)))
 
Share this answer
 
just simply use the below type expesstion to convert the label text string to integer....

int.parse(text);

or

int32.convert(text.tostring());
 
Share this answer
 

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