0

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); } } } } 
1
  • 1
    please improve your question: where/which errors you get?, try to decrease your code size, only a small part around the error would be needed. Commented Jun 9, 2011 at 3:34

4 Answers 4

4

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); 
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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.

2 Comments

Thanks for the reply. I've tried putting the reservationid back but it still have an error: Input string was not in a correct format so I'm guessing its either reservationid or one of the 3 parameters is the culprit. I'll try to check again..
if it is not auto incremented, then you have to pass the Integer value that is not already exist in the table.
0

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; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.