Loupi
Loupi

Reputation: 1112

Input string was not in a correct format

I'm trying to make a simple application form were user can input data like 'reservationid', 'bookid', 'EmployeeID' and 'reservedate'. Its from my program Library System. 'reservationid' is an auto increment primary key while the rest are BigInt50, NVarChar50 and DateTime10 respectively. So I'm having this error: Input String was not in a correct format. It worked fine a while ago until I modified the 'reservationid' to auto increment so where did I go wrong? I've attached a sample of my code behind.

Any help would be greatly appreciated! Thanks in advance!

namespace LibraryManagementSystemC4.User
{
    public partial class Reserving : System.Web.UI.Page
    {
        public string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["LibrarySystemConnectionString"].ConnectionString;
        }
        //string reservationid
        private void ExecuteInsert(string bookid, string EmployeeID, string reservedate)
        {
            SqlConnection conn = new SqlConnection(GetConnectionString());

            string sql = "INSERT INTO BookReservation (reservationid, bookid, EmployeeID, reservedate) VALUES " + " (@reservationid, @bookid, @EmployeeID, @reservedate)";
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] param = new SqlParameter[3];

                //param[0] = new SqlParameter("@reeservationid", SqlDbType.Int, 50);
                param[0] = new SqlParameter("@bookid", SqlDbType.BigInt, 50);
                param[1] = new SqlParameter("@EmployeeID", SqlDbType.NVarChar, 50);
                param[2] = new SqlParameter("@reservedate", SqlDbType.DateTime, 10);

                //param[0].Value = reservationid;
                param[0].Value = bookid;
                param[1].Value = EmployeeID;
                param[2].Value = reservedate;

                for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }

                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }

            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Insert error";
                msg += ex.Message;
                throw new Exception(msg);
            }

            finally
            {
                conn.Close();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (reservationidTextBox != null)
            {
                //reservationidTextBox.Text
                ExecuteInsert(bookidTextBox.Text, EmployeeIDTextBox.Text, reservationidTextBox.Text);

                ClearControls(Page);
            }

            else
            {
                Response.Write("Please input ISBN");
                bookidTextBox.Focus();
            }

            {
                //get bookid from Book Details and Employee PIN from current logged-in user
                bookidTextBox.Text = DetailsView1.SelectedValue.ToString();
                EmployeeIDTextBox.Text = HttpContext.Current.User.Identity.ToString();
            }
        }

        public static void ClearControls(Control Parent)
        {
            if (Parent is TextBox)
            {
                (Parent as TextBox).Text = string.Empty;
            }

            else
            {
                foreach (Control c in Parent.Controls)
                    ClearControls(c);
            }
        }
    }
}

Upvotes: 0

Views: 1817

Answers (4)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

Its because you are not passing the reservationid an Integer value to your command parameters when it is not Auto Increment.

I can see from your code, that you have declared string reservationid, but you are not assigning it any value and secondly it should an integer value.

Upvotes: 1

Robert Beveridge
Robert Beveridge

Reputation: 1

I know this is deeply necro-posted, but since it seems from Loupi's comment on 9Jun11 that he was still having problems, I'd post the actual answer. Bala's answer was what was still giving him the Input Type is not in a correct format error; using a Convert.ToInt64 statement in a value assignation was tripping it. Do the conversion in variables previous to assigning the parameter values and it works a charm. The most likely culprit is that bookid was some sort of non-zero empty string representation (blank quotes, a space, null, whatever).

Edit: A quick and easy one-line test that's relatively bulletproof:

long numAccountNum = Int64.TryParse(AccountNum, out numAccountNum) ? Convert.ToInt64(AccountNum) : 0;

Upvotes: 0

Bala R
Bala R

Reputation: 109027

If reservationid is auto incremented then remove it from your insert query

string sql = "INSERT INTO BookReservation ( bookid, EmployeeID, reservedate) VALUES (@bookid, @EmployeeID, @reservedate)";

also try

            param[0].Value = Convert.ToInt64(bookid);
            param[1].Value = EmployeeID;
            param[2].Value = Convert.ToDate(reservedate);

Upvotes: 4

Mujah Maskey
Mujah Maskey

Reputation: 8804

after you made reservationid to autoincrement then you dont have to do like

string sql = "INSERT INTO BookReservation (reservationid, bookid, EmployeeID, reservedate) VALUES " + " (@reservationid, @bookid, @EmployeeID, @reservedate)";

remove reservationid to insert. do like

string sql = "INSERT INTO BookReservation ( bookid, EmployeeID, reservedate) VALUES (@bookid, @EmployeeID, @reservedate)";

Upvotes: 2

Related Questions