I am inserting user data into the sql table, but on submitting it gives the following error.
System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.Convert.ToDateTime(String value) at Registration_Requestaccount.BtnSubmit_Click(Object sender, EventArgs e) in e:\ECCII\Registration\Requestaccount.aspx.cs:line 166
Here is my code:
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
<asp:TextBox ID="tbxpartyjoinson" runat="server" CssClass="TextBoxClass" Width="50%" TextMode="SingleLine" ToolTip="Enter Your Email Address" AutoCompleteType="Disabled" placeholder="E.g. 01-03-2010"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="tbxpartyjoinson" PopupPosition="Right" Format="dd-MM-yyyy"></ajaxToolkit:CalendarExtender>
<asp:RequiredFieldValidator ID="rvjoiningdate" runat="server" Display="None" ControlToValidate="tbxpartyjoinson" ErrorMessage="Please Enter Your Date of Joining the Party" Text="*" Font-Names="Calibri" Font-Size="medium" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revjoiningdate" runat="server" Display="None" ControlToValidate="tbxpartyjoinson" ValidationExpression="^(0[1-9]|1[0-9]|2[0-9]|3[1-2])[-](0[1-9]|1[0-2])[-](1[9][0-9][0-9]|2[0][0-9][0-9])$" ErrorMessage="Date should be in the format 31/12/2014" Text="*" Font-Names="Calibri" Font-Size="Small" ForeColor="Red"></asp:RegularExpressionValidator>
db1.sqlcmd = new SqlCommand("uspAccountRequest");
using (SqlDataAdapter sda = new SqlDataAdapter())
{
db1.sqlcmd.CommandType = CommandType.StoredProcedure;
db1.sqlcmd.Parameters.AddWithValue("@FirstName", Firstname);
db1.sqlcmd.Parameters.AddWithValue("@LastName", Lastname);
db1.sqlcmd.Parameters.AddWithValue("@CNIC", cnic);
db1.sqlcmd.Parameters.AddWithValue("@Email", email);
db1.sqlcmd.Parameters.AddWithValue("@Party", DDParty.SelectedValue);
db1.sqlcmd.Parameters.AddWithValue("@PartyJoiningDate", Convert.ToDateTime(tbxpartyjoinson.Text));
db1.sqlcmd.Parameters.AddWithValue("@Constituency", DDConstituency.SelectedValue);
db1.sqlcmd.Parameters.Add("@success", SqlDbType.Bit);
db1.sqlcmd.Parameters["@success"].Direction = ParameterDirection.Output;
db1.sqlcmd.Connection = db1.sqlcon;
db1.sqlcon.Open();
reqid = Convert.ToInt32(db1.sqlcmd.ExecuteScalar());
success = Convert.ToBoolean(db1.sqlcmd.Parameters["@success"].Value);
CREATE PROC uspAccountRequest
@success bit out,
@FirstName varchar(20), @LastName varchar(20), @CNIC bigint, @Email varchar(50),@Constituency int,@Party int,@PartyJoiningDate datetime
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
IF EXISTS(SELECT Email FROM TblAccountRequest WHERE Email = @Email)
BEGIN
SELECT -1
END
ELSE IF EXISTS (SELECT CNIC FROM TblAccountRequest WHERE CNIC = @CNIC)
BEGIN
SELECT -2
END
ELSE IF EXISTS(SELECT Email FROM TblUser WHERE Email = @Email)
BEGIN
SELECT -3
END
ELSE IF EXISTS(SELECT NIC FROM TblUser WHERE NIC = @CNIC)
BEGIN
SELECT -4
END
ElSE
BEGIN
Insert into TblAccountRequest (FirstName, LastName, CNIC, Email, Constituency, Party, partyjoninson, RequestDate, IsAccepted)
Values (@FirstName,@LastName,@CNIC,@Email,(SELECT Code FROM TblConstituency WHERE ConstID = @Constituency),(SELECT PartyName FROM TblParty WHERE PartyId = @Party),@PartyJoiningDate,GETDATE(),0) SELECT @@IDENTITY
SELECT SCOPE_IDENTITY()
SET @success = 1
END
END TRY
BEGIN CATCH
SET @success = 0
END CATCH
END