.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Detailsview insert

Posted By:      Posted Date: October 12, 2010    Points: 0   Category :ASP.Net

I have a detailsview in insert mode. I want to prompt the user to enter data in all fields, if left empty. Could someone please find a few minutes to show me how to do it?

Thanks in advance.

View Complete Post

More Related Resource Links

DetailsView get key after insert

Hello!I'm trying to catch (in string "s") the primary key for the record being inserted via DetailsView. I have an SqlDataSource connection to an Access 2003 database. The primary key is auto-increment integer (long) field.Code:protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e) { OleDbParameter insertedKey = new OleDbParameter("@KontaktID", OleDbType.Integer); insertedKey.Direction = ParameterDirection.Output; e.Command.Parameters.Add(insertedKey); } protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e) { DbCommand command = e.Command; string s = command.Parameters["@KontaktID"].Value.ToString(); }generates an error:System.Data.OleDb.OleDbDataAdapter internal error: invalid parameter accessor: 12 BADBINDINFO.I can not figure out what I'm doing wrong??Thx in advance!

Can I use a DetailsView control to insert and update with RIA services?



I'm struggling with trying to use RIA services from an ASP.NET web site.

I created a new web site, added an entity framework model, then added a RIA service that uses that model. I used the DomainDataSource control to bind the ASP.NET controls to the RIA service.

I have a GridView that shows items from the database (via RIA and the entity framework model underneath it), and can see the items fine. I added a DetailsView as a pop-up, as shown in http://mattberseth.com/blog/2007/07/modalpopupextender_example_for.html and http://mattberseth.com/blog/2008/04/masterdetail_with_the_gridview.html. When I click the "edit" link in a row in the GridView, the DetailsView shows with the right data, but clicking the "save" link doesn't actually update the database.

Furthermore, I can't work out how to add new items this way, as when I try and use the DetailsView to add a new item, nothing is sent to the database.

Anyone any ideas how this can be done?


DropDownLists inside Detailsview INSERT cant get to work.



I'm having a DetailsView which has two DropDownList's inside so I should be able to pick 2 values for the field from DDL instead of writing something for it. But I cant get it to Insert any new values to my Database...

Can anyone help me with this? I'm pasting my code and table details here below.

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
        CellPadding="4" DataKeyNames="SignUpKey" DataSourceID="LinqDataSource1"
        DefaultMode="Insert" ForeColor="#333333" GridLines="None" Height="50px"
        Width="125px" oniteminserted="DetailsView1_ItemInserted"
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

Insert command not using selected value from DDL in a DetailsView


My web form contains a detailsview control that I have added a templated field to with a DropDownList in the InsertItemTemplate. The DetailsView default mode is 'insert. My detailsview is attached to a sqldatasource that has a stored procedure in the db to handle the insert. When I enter data in the fields and select insert all values from the detailsview are written to the table except for the selection that was made in the dropdownlist. I am using the ddl.selecteditem.value to populate my variable however when the insert method is envoked it doesn't send my value. The DDL value that I am capturing does not need to be written to the table it is just a flag for some variables that need to be set at the stored proc level. How do I get the DDL selected value to be sent with the insert statement? Any help would be greatly appreciated.

Here is my code behind:





Protected Sub

DropDownList1_SelectedIndexChanged(ByVal sender

FindControl In DetailsView Edit & Insert ItemTemplate


Hey,guys. I got a problem about finding controls in Edit/Insert Itemtemplate inside a DetailsView.

Here is the code.

I put a Label (id= "Label6") to test whether the findcontrol method works or not. Unfortunately, I almost tried every means but still coudn't find it.

protected void Page_Load(object sender, EventArgs e)

  //Edit Button OnCommand
  protected void ButtonEditOnCommand(object sender, CommandEventArgs e)
  if (e.CommandName == "Edit")

  // Update Button OnCommand
  protected void ButtonUpdateOnCommand(object sender, CommandEventArgs e)
  if (e.CommandName == "Update")
  string sql = "UPDATE StudentInfo SET StudentName=@studentname, Score=@score, Grade=@grade, Remarks=@remarks WHERE ID=@id";
  SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconn"].ToString());
  SqlCommand cmd = new SqlCommand(sql, conn);

  // find controls
  if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
  TextBox EditStudentName = (TextBox)DetailsView1.FindControl("TextBoxEditStudentName");
  TextBox EditScore = (TextBox)DetailsView1.FindControl("TextBoxEditScore");
  TextBox EditGrade

DetailsView - Cannot insert the value NULL into column


Can somebody tell me why I am getting this error when I try to insert data in a DetailsView 2.0? 

Cannot insert the value NULL into column 'Title', table 'C:\INETPUB\WWWROOT\TEST\APP_DATA\QUIZ.MDF.dbo.Question'; column does not allow nulls. INSERT fails.

I used the Configure Data Source wizard to set this up and Question ID is a autonumber.

When you autogenerate the Insert button on a Detailsview control and click on that button during run time the control posts back with text input areas where you are supposed to type the Title, Answer1, Answer2 etc. Obviously I typed data in to this text area labeled Title. It didn't work and that's why I'm asking about the control. Because I can't debug the control I am having a hard time figuring out where the problem is.

Here is the code and thanks for any help.

<asp:DetailsView ID="EditDetails" runat="server" Height="50px" Width="550px" AutoGenerateRows="False"  AutoGenerateInsertButton="True" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" AllowPaging="True">
                        <FooterStyle BackColor="#5D7B9D" Font-Bol

DetailsView Get Values on Insert. Major Problem :(



I'm having a problem to get the inserted values from the detailsview. I've tried the following things:

           // Get Value from DropDown
        int TopicID = 0;
        foreach (DetailsViewRow row in ItemsDetailsView.Rows)
            if (row.Cells[0].Text == "Topic")
                 DropDownList TopicDropDown = (DropDownList)row.Cells[1].FindControl("Topic_TopicIDList");
                 TopicID = int.Parse(TopicDropDown.SelectedValue.ToString());


            MsgBox(CType(DetailsView1.Rows(1).Cells(0).Controls(0), Label))

            For Each row As DetailsViewRow In DetailsView1.Rows

                Dim cell1 As TableCell = row.Cells(1)

                Dim value1 As String

                value1 = cell1.Text



            Dim item As New DetailsViewItem(col.ColumnName, row(index).ToString(), col.DataType.ToString(), col.DefaultValue)


            tb = DetailsView1.FindControl("Categorie")
            s = tb.Text


            For Each dvr As DetailsViewRow In DetailsView1.Rows
                If dvr.Cells.Count = 2 Th

can't insert from a dropdownlist within a detailsview


OK, I figure that this should be relatively straightforward, but I still can't get it to work. I have a gridview and a detailsview to set up shifts for students and teachers. I would like to be able to insert new shifts using a detailsview. I will limit the students, teachers and location to items that are in a dropdownlist. 

The problem is that when I handle the iteminserting event. the e.values collection doesn't contain all of the fields I want to populate. How do I add the new fields to the list. 

This is my codebehind:

    Sub On_Inserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs)
        e.Values("student") = tbStudent.SelectedValue
        e.Values("shift_location") = CType(dvNewShift.FindControl("newlocation"), DropDownList).SelectedValue

Insert record in nested detailsview


Scenario : I have two database tables with one to many relationship. eg:- City and Related news. So I have a gridview(Gridview1) which lists out all the cities and allows edit, delete of the gridview rows. Now since i wanted to display all news for the city , i have a nested gridview as a column in this Gridview1.  This nested gridview displays all the news for the cities. And I have edit, delete buttons for this gridview too - So far, good - works fine.

Now what I would like to do is - To insert a new news item for a city. For this, I attached a detailsview to my nested grid(gridview2), and tried to insert. I need the primary key id of city table from the first grid (gridview1) to insert the news into the second database table and I could not access this value from the parent gridview.  My gridview1 does not have datakeyname, it is programatically bound.

Also, When the gridview/detailsview is nested, it does not get listed in the event-handler list. So I cannot use say, detailsview1_iteminserting. I do not how to add this event handler

Has anybody done anything similar? When I started, it looked like a pretty straightforward thing - There must be some way to do this. But I just can't get it to work.  Any ideas? How do you usually insert into the child table in a one-to-many relationship? Should i jus

Master-Detail with the GridView, DetailsView and jQuery's ThickBox

One more article about grids from Matt Berseth. This time he shows how to create master-detail with the gridview, detailsview and jQuery's ThickBox. He writes:
So I thought I would check some of this out and along the way rebuild my Master-Detail with the GridView, DetailView and ModalPopup Controls and replace the ModalPopup with jQuery's ThickBox. I found it pretty interesting - read on to see how it went

Insert value using Table Value Functions

a real gem in Sql Server 2008. mostly people still using Stored procedure may be they shifted to SQL Server but they are not using TVF right now.

Insert Rows with a GridView

In ASP.NET version 1.x, I used the footer on the DataGrid control to insert new rows by placing a series of TextBox, DropDownLists, CheckBoxes etc. controls on it and then handling a save button. This also works with the GridView in version 2.0 but with one major exception: if your data source is empty, the GridView will hide your footer (and header) and only display the EmptyDataText.

SqlCommand.ExecuteNonQuery() returns -1 when doing Insert / Update / Delete

Sometimes you end up with a return value of -1 when using the SqlClient.SqlCommand.ExecuteNonQuery method.

Why is that?

Well, the ExecuteNonQuery method is there for statements for changing data, ie. DELETE / UPDATE /INSERT, and the returned value are the number of rows affected by that statement.

When checking the documentation we can see that there are some conditions that return -1.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of

rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

Insert Error not helpful



I am using VS2010

When I try to updated or insert a record in my User table I am getting an error that does not make sense to me and I cannot figure out what is wrong.

Also, when I try to insert a new record I get a validation error saying that the Id is required. However it is an identity field in the db so it should not be necessary to send it to the server.

Could you please shed some light?


An error has occurred.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Microsoft.Web.UI.WebControls.EntityOperationException: An error has occurred.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace:

[EntityOperationException: An error has occurred.]
   Microsoft.Web.UI.WebControls.DomainDataSourceView.PerformCudOperation(ChangeSetEntry operation, Action`1 operationPerformingEvent, Action`1 operationPerformedEvent) +331
   Microsoft.Web.UI.WebControls.DomainDataSourceView.UpdateObject(Object oldEntity, O

Detecting Edit or Insert Page


I am coding a .cs file that handles the metadata for a particular table. I am putting a custom validation that allows me to update or insert only if the PubMedID is not already in the database. This would be easy except that the PMID should also be allowed to be null as some of the references in the database were not from PubMed. So i perform a query on the database to see if there is already an identical PMID in the database. This works great when using Insert. However, when using Edit, it does not. The PMID is already in the database because the one currently being edited has the same PMID!!! So I know exactly how to change my query etc. However, I do not know how to tell from inside this Edit/Insert agnostic page, which one it is being called from. Thanks for your help! 

how To Insert the single quotes in sqlcommand in asp.net


Hi all

    i am developed on simple user registeration application in that i gave  address field .when i am trying to enter something like this (1st main,b'lore) but wont take while inserting.

my query is

sqlcommand cmd=new sqlcommand("insert into registeration_form(name,address)values('"+txtname.text+"','"+txt_address+"')",con);

how can i pass the  quotes also to my query please if any one know reply me.


G Dinakaran

Insert data to Excel using OLEDB



I am trying to insert data to Excel Document using OLEDB as follows:-

 sql = "Insert into [MyFirstSheet$] (" + Title1 + "," +Title2 + "," + Title3 + ") 
values('" + Value1 + "','" + Value2 + "','" + Value2 + "')";
myCommand.CommandText = sql;

 sql = "Insert into [MyFirstSheet$] (" + Title1 + "," +Title2 + "," + Title3 + ") 

values('" + Value1 + "','" + Value2 + "','" + Value2 + "')";

myCommand.CommandText = sql;


But, the Title1 ,Title2, Title3 are in the 3rd row of the Excel Document. So, the data is not inserted.

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend