Click here to Skip to main content
14,133,823 members
Rate this:
 
Please Sign up or sign in to vote.
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:-
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:-

[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 :-
@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
NotPolitcallyCorrect 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?
NotPolitcallyCorrect 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);
}
}
Bryian Tan 26-Mar-17 17:09pm
   
Your original code look fine. It should work, here what I recommend you to check. On the EDMX diagram, find the CenterTechnical Model. Click on the ID property, right click and select Properties, make sure the StoreGeneratedPattern = None

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190518.1 | Last Updated 26 Mar 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100