Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want save value in sql server database but i have this error , but i don't use identity
i want save value that i write in textbox.

this is CenterLocation model class:-
C#
public partial class CenterTechnical
  {
      public int ID { get; set; }
      public string LocationCenterID { get; set; }
      public string TechName { get; set; }
      public string TechMobile { get; set; }
      public string TechPhone { get; set; }
      public string TechAddress { get; set; }

      public virtual LocationCenter LocationCenter { get; set; }
  }


and this is create methos in controller:-

C#
[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID, LocationCenterID, TechName, TechMobile, TechPhone, TechAddress")]CenterTechnical centerTechnical)
        {
            if(ModelState.IsValid)
            {
                var newTechnical = new CenterTechnical()
                {
                    ID = centerTechnical.ID,
                    LocationCenterID = centerTechnical.LocationCenterID,
                    TechName = centerTechnical.TechName,
                    TechMobile = centerTechnical.TechMobile,
                    TechPhone = centerTechnical.TechPhone,
                    TechAddress = centerTechnical.TechAddress
                };
                db.CenterTechnical.Add(newTechnical);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            else
            {
                return View(centerTechnical);
            }
        }


View :-
HTML
@using (Html.BeginForm("Create", "CenterTechnical", FormMethod.Post, new { enctype = "form-data" }))
{
    @Html.AntiForgeryToken()
    <div class="row">
        <div class="col-md-12">
            <div class="box box-DG">
                <div class="box-header with-border">
                    <span style="color:#2D69B7"></span>
                    <h3 class="box-title">@WhiteWhaleLanguage.Technical</h3>
                </div>
                <div class="box-body">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-horizontal">
                                <div class="form-group">
                                    @Html.LabelFor(model => model.ID, @WhiteWhaleLanguage.TechnicalID, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-6">
                                        <div class="">
                                            @Html.TextBoxFor(model => model.ID, new { @class = "form-control" })
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.LabelFor(model => model.LocationCenterID, @WhiteWhaleLanguage.CenterCode, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-6">
                                        <div class="">
                                            @Html.TextBoxFor(model => model.LocationCenterID, new { @class = "form-control" })
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.LabelFor(model => model.TechName, @WhiteWhaleLanguage.TechName, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-6">
                                        <div class="">
                                            @Html.TextBoxFor(model => model.TechName, new { @class = "form-control" })
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.LabelFor(model => model.TechMobile, @WhiteWhaleLanguage.TechMobile, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-6">
                                        <div class="">
                                            @Html.TextBoxFor(model => model.TechMobile, new { @class = "form-control" })
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.LabelFor(model => model.TechPhone, @WhiteWhaleLanguage.TechPhone, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-6">
                                        <div class="">
                                            @Html.TextBoxFor(model => model.TechPhone, new { @class = "form-control" })
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.LabelFor(model => model.TechAddress, @WhiteWhaleLanguage.TechAddress, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        <div class="">
                                            @Html.TextAreaFor(model => model.TechAddress, new { @class = "form-control", @rows = "5" })
                                        </div>
                                    </div>
                                </div>
                                <br />
                                <div class="modal-footer with-border">
                                    <input type="submit" name="" value=@WhiteWhaleLanguage.SaveNewTechnical , class="btn btn-primary col-md-2" />
                                    @Html.ActionLink(WhiteWhaleLanguage.BackToList, "Index", null, new { @class = "btn btn-default" })
                                </div>
                            </div>    
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
}


What I have tried:

i want add value in textbox and save values and i don't use identity
Posted
Comments
[no name] 26-Mar-17 14:25pm    
The error message is self explanatory. So what is your question?
MahmoudOmar 26-Mar-17 14:28pm    
Why when i write number Like 2 in textboxFor ID its give me This error?
[no name] 26-Mar-17 14:39pm    
You would find that out by learning how to debug your code. Obviously whatever you enter is not making it and you are getting a null value.
Bryian Tan 26-Mar-17 14:50pm    
Did you confirm centerTechnical.ID is not null?
MahmoudOmar 26-Mar-17 15:43pm    
it's Primary Key but i don't make it identity , but when i used query string its worked. why i don't know !!!
this is Edite Code:-
[AuthorizeRole(nameof(CenterTechnical), nameof(Create))]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(/*[Bind(Include = "ID, LocationCenterID, TechName, TechMobile, TechPhone, TechAddress")]*/CenterTechnical centerTechnical)
{
if(ModelState.IsValid)
{
string constr = ConfigurationManager.ConnectionStrings["WhiteWhaleModel"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = " INSERT INTO CenterTechnical(ID, LocationCenterID,TechName,TechMobile,TechPhone,TechAddress) ";
query = query + " VALUES(@ID, @LocationCenterID, @TechName, @TechMobile, @TechPhone, @TechAddress) ";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@ID", centerTechnical.ID);
cmd.Parameters.AddWithValue("@LocationCenterID", centerTechnical.LocationCenterID);
cmd.Parameters.AddWithValue("@TechName", centerTechnical.TechName);
cmd.Parameters.AddWithValue("@TechMobile", centerTechnical.TechMobile);
cmd.Parameters.AddWithValue("@TechPhone", centerTechnical.TechPhone);
cmd.Parameters.AddWithValue("@TechAddress", centerTechnical.TechAddress);
cmd.ExecuteNonQuery();
con.Close();
}
}
//var newTechnical = new CenterTechnical()
//{
// ID = centerTechnical.ID,
// LocationCenterID = centerTechnical.LocationCenterID,
// TechName = centerTechnical.TechName,
// TechMobile = centerTechnical.TechMobile,
// TechPhone = centerTechnical.TechPhone,
// TechAddress = centerTechnical.TechAddress
//};
//db.CenterTechnical.Add(newTechnical);
//db.SaveChanges();

return RedirectToAction("Index");
}
else
{
return View(centerTechnical);
}
}

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