Mark Tabor

Mark Tabor

  • 587
  • 2k
  • 460.6k

an item with same key already exists

Oct 12 2016 5:26 AM
I have a simple mvc website now i want to create a user registration process when i click the register button after putting all the values for user like name,email,address,phone then my code is not going to the controller to debug instead it gives the error"An item with the same key has already been added."
below is my model i have checked that no two properties have the same name .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Web.Mvc;
using System.Drawing;
using System.Drawing.Imaging;

namespace BusinessApplicationTemplate.Models
{

public class Login
{
[Required(ErrorMessage = "Email Id Required")]
[DisplayName("Email ID")]
[RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$",
ErrorMessage = "Email Format is wrong")]
[StringLength(50, ErrorMessage = "Less than 50 characters")]
public string EmailId
{
get;
set;
}

[DataType(DataType.Password)]
[Required(ErrorMessage = "Password Required")]
[DisplayName("Password")]
[StringLength(30, ErrorMessage = ":Less than 30 characters")]
public string Password
{
get;
set;
}
//[Display(Name = "Remember me?")]
//public bool RememberMe { get; set; }
public bool IsUserExist(string emailid, string password)
{
bool flag = false;
SqlConnection connection = new SqlConnection(
System.Configuration.ConfigurationManager.ConnectionStrings["cs"].ConnectionString);
connection.Open();
SqlCommand command = new SqlCommand("select count(*) from Users where userID='" + emailid + "' and Password='" + password + "'", connection);
flag = Convert.ToBoolean(command.ExecuteScalar());
connection.Close();
return flag;
}

public class CaptchImageAction : ActionResult
{
public Color BackgroundColor { get; set; }
public Color RandomTextColor { get; set; }
public string RandomText { get; set; }
public override void ExecuteResult(ControllerContext context)
{
RenderCaptchaImage(context);
}
private void RenderCaptchaImage(ControllerContext context)
{
Bitmap objBmp = new Bitmap(150, 60);
Graphics objGraphic = Graphics.FromImage(objBmp);
objGraphic.Clear(BackgroundColor);
SolidBrush objBrush = new SolidBrush(RandomTextColor);
Font objFont = null;
int a;
string myFont, str;
string[] crypticsFont = new string[11];
crypticsFont[0] = "Times New roman";
crypticsFont[1] = "Verdana";
crypticsFont[2] = "Sylfaen";
crypticsFont[3] = "Microsoft Sans Serif";
crypticsFont[4] = "Algerian";
crypticsFont[5] = "Agency FB";
crypticsFont[6] = "Andalus";
crypticsFont[7] = "Cambria";
crypticsFont[8] = "Calibri";
crypticsFont[9] = "Courier";
crypticsFont[10] = "Tahoma";
for (a = 0; a < RandomText.Length; a++)
{
myFont = crypticsFont[a];
objFont = new Font(myFont, 18, FontStyle.Bold | FontStyle.Italic |
FontStyle.Strikeout);
str = RandomText.Substring(a, 1);
objGraphic.DrawString(str, objFont, objBrush, a * 20, 20);
objGraphic.Flush();
}
context.HttpContext.Response.ContentType = "image/GF";
objBmp.Save(context.HttpContext.Response.OutputStream, ImageFormat.Gif);
objFont.Dispose();
objGraphic.Dispose();
objBmp.Dispose();
}
}
}

public class Register
{
[Required(ErrorMessage = "FirstName Required:")]
[DisplayName("First Name:")]
[RegularExpression(@"^[a-zA-Z'.\s]{1,40}$", ErrorMessage = "Special Characters not allowed")]
[StringLength(50, ErrorMessage = "Less than 50 characters")]
public string FirstName { get; set; }

[Required(ErrorMessage = "LastName Required:")]
[RegularExpression(@"^[a-zA-Z'.\s]{1,40}$", ErrorMessage = "Special Characters not allowed")]
[DisplayName("Last Name:")]
[StringLength(50, ErrorMessage = "Less than 50 characters")]
public string LastName { get; set; }

[Required(ErrorMessage = "EmailId Required:")]
[DisplayName("Email Id:")]
[RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$",
ErrorMessage = "Email Format is wrong")]
[StringLength(50, ErrorMessage = "Less than 50 characters")]
public string EmailIds { get; set; }

[Required(ErrorMessage = "Password Required:")]
[DataType(DataType.Password)]
[DisplayName("Password:")]
[StringLength(30, ErrorMessage = "Less than 30 characters")]
public string psword { get; set; }

[Required(ErrorMessage = "Confirm Password Required:")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Confirm not matched.")]
[Display(Name = "password:")]
[StringLength(30, ErrorMessage = "Less than 30 characters")]
public string ConfirmPassword { get; set; }

[Required(ErrorMessage = "Street Address Required")]
[DisplayName("Street Address1:")]
[StringLength(100, ErrorMessage = "Less than 100 characters")]
public string StreetAdd1 { get; set; }

[DisplayName("Street Address2:")]
[StringLength(100, ErrorMessage = "Less than 100 characters")]
public string StreetAdd2 { get; set; }

[Required(ErrorMessage = "City Required")]
[DisplayName("City:")]
[RegularExpression(@"^[a-zA-Z'.\s]{1,40}$", ErrorMessage = "Special Characters not allowed")]
[StringLength(50, ErrorMessage = "Less than 50 characters")]
public string City { get; set; }

[Required(ErrorMessage = "State Required")]
[DisplayName("State:")]
[RegularExpression(@"^[a-zA-Z'.\s]{1,40}$", ErrorMessage = "Special Characters not allowed")]
[StringLength(50, ErrorMessage = "Less than 50 characters")]
public string State { get; set; }

[Required(ErrorMessage = "ZipCode Required")]
[DisplayName("Zip Code:")]
[StringLength(20, ErrorMessage = "Less than 20 characters")]
public string Zip { get; set; }

[Required(ErrorMessage = "Enter Verification Code")]
[DisplayName("Verification Code:")]
public string Captcha { get; set; }
public string Remembermes { get; set; }
public bool IsUserExist(string emailid)
{
bool flag = false;
SqlConnection connection = new SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings["cs"].ConnectionString);
connection.Open();
SqlCommand command = new SqlCommand("select count(*) from Users where userID='" + emailid + "'", connection);
flag = Convert.ToBoolean(command.ExecuteScalar());
connection.Close();
return flag;
}

public bool Insert()
{
bool flag = false;
if (!IsUserExist(EmailIds))
{
SqlConnection connection = new SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings["cs"].ConnectionString);
connection.Open();
SqlCommand command = new SqlCommand("insert into Users values('" + FirstName + "','" + LastName + "','" + EmailIds + "','" + psword + "', '" + StreetAdd1 + "','" + StreetAdd2 + "','" + City + "','" + State + "', '" + Zip + "')", connection);
flag = Convert.ToBoolean(command.ExecuteNonQuery());
connection.Close();
return flag;
}
return flag;
}
}
}

What I have tried:

Basically i have two classes in one class check the above code i have inspact it and icould not found any same name for properties

Answers (5)