Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdown List in a Formview

Status
Not open for further replies.

barrylowe

Programmer
Nov 6, 2001
188
GB
I am tearing my hair out over a problem with drop-down lists. I am trying to use one in my main prject but have had such problems that I have created a test page just containing a single drop-down list to see fi I can figure out the problem.



Basically I have a test table with 2 fields: DDL_Test_ID (ID field, autogenerated) and TestValue (varchar 50)

On my page I have a formview which contains a dropdown list in the InsertItem template which I want to use to write a value into the TestValue field in my database table. However when I run the page and click insert I get the following error page:

Cannot insert the value NULL into column 'TestValue', table 'WellManClinics.dbo.DDLtest'; column does not allow nulls. INSERT fails.
The statement has been terminated.

I am obviously missing somethign very obvious here but for the life of me I can't figure it out. Can anyone help? See code below:


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DDL_Test.aspx.vb" Inherits="DDL_Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="<head runat="server">
<title>Drop-down List test</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:FormView ID="FormView1" runat="server" DataKeyNames="DDL_Test_ID"
DataSourceID="SqlDataSource1" DefaultMode="Insert">
<EditItemTemplate>
DDL_Test_ID:
<asp:Label ID="DDL_Test_IDLabel1" runat="server"
Text='<%# Eval("DDL_Test_ID") %>' />
<br />
TestValue:
<asp:TextBox ID="TestValueTextBox" runat="server"
Text='<%# Bind("TestValue") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
&nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
TestValue:
<asp:DropDownList ID="TestValue" runat="server"
SelectedIndex='<%# Bind("DDL_Test_ID") %>' AutoPostBack="True">
<asp:ListItem Value="V1">Value 1</asp:ListItem>
<asp:ListItem Value="V2">Value 2</asp:ListItem>
<asp:ListItem Value="V3">Value 3</asp:ListItem>
<asp:ListItem Value="V4">Value 4</asp:ListItem>
<asp:ListItem Value="V5">Value 5</asp:ListItem>
</asp:DropDownList>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
&nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
DDL_Test_ID:
<asp:Label ID="DDL_Test_IDLabel" runat="server"
Text='<%# Eval("DDL_Test_ID") %>' />
<br />
TestValue:
<asp:Label ID="TestValueLabel" runat="server" Text='<%# Bind("TestValue") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
&nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" />
&nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:WellManClinicsConnectionString %>"
DeleteCommand="DELETE FROM [DDLtest] WHERE [DDL_Test_ID] = @original_DDL_Test_ID AND [TestValue] = @original_TestValue"
InsertCommand="INSERT INTO [DDLtest] ([TestValue]) VALUES (@TestValue)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [DDL_Test_ID], [TestValue] FROM [DDLtest]"
UpdateCommand="UPDATE [DDLtest] SET [TestValue] = @TestValue WHERE [DDL_Test_ID] = @original_DDL_Test_ID AND [TestValue] = @original_TestValue">
<DeleteParameters>
<asp:parameter Name="original_DDL_Test_ID" Type="Int32" />
<asp:parameter Name="original_TestValue" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="TestValue" Type="String" />
<asp:parameter Name="original_DDL_Test_ID" Type="Int32" />
<asp:parameter Name="original_TestValue" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="TestValue" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>
 
Hello!

Are you sure the DDL_Test_ID column in the DDLtest table has IDENTITY set on?
You could start the SQL Profiler, trace the call to the SQL Server and see what is sent. (From Microsoft SQL Server Management Studio, select option SQL Server Profiler from Tools menu, then File -> New Trace ...)

[morning]
 
The problem here is that you are using a datasource control. As you can see, when something doesn't work right, you can't debug the problem. You are best off writing all the DB code yourself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top