0

I am having a problem dealing with Detailsview InsertItemTemplate. I am following ASP.Net 4 for Beginners by WROX.

I have a dbase with table Employee(EmpId, Name, DoB, Sex, DeptId, Position) I am trying to insert values into this table using Detailsview.

Using the regular detailsview I could insert all the values. It works fine.But when I converted the detailsview to template, there is a problem.

What I did?

-On Smart tag of Detailsview, I clicked Edit Fields. Delete all the avaible fields except EmpID and added Template Field from Available Fields. -Again on Smart Tag of Detailsview, clicked Edit Template and selected InsertItemTemplate in DropDownList. -Using Standard components from Toolbox, I added text box for Name and dropdownlist for other fields. (I have not deleted EmpId, so I do not need one for it). -Here is the rest of Markup view:

Enter following information to register: Name:  
DoB:      
Sex:
DeptId:
Position:

-And here is the code behind CS file for Insert event for detailsview

protected void DetailsView1_ItemInserting1(object sender, DetailsViewInsertEventArgs e) { string id = System.Guid.NewGuid().ToString(); TextBox txt_name = (TextBox)DetailsView1.FindControl("EmpId"); e.Values["EmpId"] = id;

    DropDownList ddl_month = (DropDownList)DetailsView1.FindControl("DDL_Month");
    string month = ddl_month.SelectedValue;
    e.Values["DDL_Month"] = month;

    DropDownList ddl_day = (DropDownList)DetailsView1.FindControl("DDL_Day");
    string day = ddl_day.SelectedValue;
    e.Values["DDL_Day"] = day;

    DropDownList ddl_year = (DropDownList)DetailsView1.FindControl("DDL_Year");
    string year = ddl_year.SelectedValue;
    e.Values["DDL_Year"] = year;

    DropDownList ddl_sex = (DropDownList)DetailsView1.FindControl("DDL_Sex");
    string sex = ddl_sex.SelectedValue;
    e.Values["DDL_Sex"] = sex;

    DropDownList ddl_pos = (DropDownList)DetailsView1.FindControl("DDL_Pos");
    string pos = ddl_pos.SelectedValue;
    e.Values["DDL_Pos"] = pos;

    DropDownList ddl_deptid = (DropDownList)DetailsView1.FindControl("DDL_DeptId");
    string deptid = ddl_deptid.SelectedValue;
    e.Values["DDL_DeptId"] = deptid;
}

-Could you also suggest me how to combine the three selected value from dropdownlist for Date of Birth and add it to database.

Thank you!!!

asp_NewBee
  • 299
  • 1
  • 6
  • 18

2 Answers2

1

With the built in .NET controls, usually you'll have problems if you try deleting fields that have been generated. Your best bet to get things working this way would be to completely delete and re-add the controls you're using, and just try changing the field that you're trying to use a template with. You may still have some issues getting this to work, but shouldn't be too difficult.

Using controls in this way tends not to be flexible, and it's difficult to fix issues. That's why most experienced developers abandon this 'easy' way of doing things, and do database updating manually by creating a form, reading the values and passing this through to a method that will carry out the relevant SQL update. It takes a while to learn but once you get the hang of it it becomes easy and solves so much difficulty trying to grapple with the very .NET specific way you need to work with controls.

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
1

To elaborate:

Yes that would be the first step. The connection string should be added to your web.config under the '' element:

http://msdn.microsoft.com/en-us/library/bf7sd233.aspx

You can then read this out in your code:

Read connection string from web.config

The following shows a reasonably good general form for doing a database update into a SQL database based on values you're taking from a form - done in C# (which I'd recommend getting started with instead of VB.NET, for various good reasons)

Update database with values from textbox

This shows how to add the connection string directly inside the code instead of from the web.config, which may be simpler for you in the first instance. This shows how to do the database update by using 'parameters' in the query (the 'variables' if you like that are denoted with @ symbols). Using parameters is basic good practise that it's worth getting used to (don't worry too much about why, but you may want to research the 'SQL injection' technique that hackers use to attack websites). This is a good starting point for you. It may seem complex at first, but ask any developer with a few years of experience and they'll tell you it's generally the way to go. As you progress in development the tempting 'out of the box' stuff provided by .NET becomes increasingly limiting, and you lose the ability to finely tune performance and other characteristics of good applications.

The example I've given is actually still relative simple. As you develop, there are other aspects you should start bringing into your code, especially creating methods that do the bit that accesses the database (by using a SQL query) in a separate class that gets called a 'Data Access Layer' (often an intermediate layer gets used called the 'Business Logic Layer' or similar, but don't worry about that yet).

Also the example I've given should really be packaging up some of the code in methods that give a clear indication of what the code is doing e.g. at a simpler level a method 'UpdateMachineDetails' could at least be used to encapsulate the code that updates the database.

There are lots of tutorials if you Google for it that show you how to update a datbase 'manually' in .NET, but the form I'm giving you puts you on a good track towards best practice that is used industry-wide by software developers.

Community
  • 1
  • 1
Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206