Updating value picked from DDL in DetailView - asp.net

(I found the way it's supposed to be done as shown in my Answer below.
I'm editing my question and deleting the code I initially put here, which was a mess, leaving just the definitions).
Win7, ASP4.5, empty_web_app in VS2013 with C#. Recreated in a very simple project accessing two tables:
1st table "Students"
student_ID
student_name
student_course_ID (is Forign Key)
2nd table "Courses"
course_ID
course_name
In my web page I have DetailView1 showing details of student who's student_ID is taken from txbStudent_ID.
DetailView1 has Edit Delete and New.
When in UPDATE or INSERT mode I need to show the course_name (rather then the course ID) in a drop-down-list and update/insert accordingly.
No code behind.
My answer below is applicable to GRIDVIEW as well.
Gadi

I was so wrong they way I coded my aspx.
So here is the right way to do it, for future beginners like I am now...
(with the help of https://msdn.microsoft.com/en-us/library/ms178294(v=vs.140).aspx)
<asp:Label ID="Label1" runat="server" Text="Student_ID"></asp:Label>
<asp:TextBox ID="txbStudent_ID" runat="server"></asp:TextBox>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="student_ID" DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="student_ID" HeaderText="student_ID" InsertVisible="False" ReadOnly="True" SortExpression="student_ID" />
<asp:BoundField DataField="student_name" HeaderText="student_name" SortExpression="student_name" />
<asp:TemplateField HeaderText="student_course_ID" SortExpression="student_course_ID">
<EditItemTemplate>
<asp:DropDownList
ID="DropDownList1"
runat="server"
DataSourceID="SqlDataSource2"
DataTextField="course_name"
DataValueField="course_ID"
SelectedValue='<%# Bind("student_course_ID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList
ID="DropDownList2"
runat="server"
DataSourceID="SqlDataSource2"
DataTextField="course_name"
DataValueField="course_ID"
SelectedValue='<%# Bind("student_course_ID") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label
ID="Label1"
runat="server"
Text='<%# Bind("student_course_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" ShowDeleteButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:Course_Student_Connection %>"
InsertCommand="INSERT INTO [Students] ([student_name], [student_course_ID]) VALUES (#student_name, #student_course_ID)"
SelectCommand="SELECT * FROM [Students] WHERE ([student_ID] = #student_ID)"
UpdateCommand="UPDATE [Students] SET [student_name] = #student_name, [student_course_ID] = #student_course_ID WHERE [student_ID] = #student_ID"
DeleteCommand="DELETE FROM [Students] WHERE [student_ID] = #student_ID">
<DeleteParameters>
<asp:Parameter Name="student_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="student_name" Type="String" />
<asp:Parameter Name="student_course_ID" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="txbStudent_ID" Name="student_ID" PropertyName="Text" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="student_name" Type="String" />
<asp:Parameter Name="student_course_ID" Type="Int32" />
<asp:Parameter Name="student_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource
ID="SqlDataSource2"
runat="server"
ConnectionString="<%$ ConnectionStrings:Course_Student_Connection %>"
SelectCommand="SELECT [course_ID], [course_name] FROM [Courses]">
</asp:SqlDataSource>
I do hope it will save some time for others.
Stackoverflow is THE greatest and by far the best Q&A site on the web!!!
Gadi.

Related

How To Insert USERID (Type: Raw, Size: 16) from Ora_aspnet_users to Another Table

Sorry I cannot upload the screenshot due to the limited number of my reputation since I am a newbie here.
ora_aspnet_user table with column name userid with datatype of Raw(16)
link to
Instruct Table with userid with datatype of Raw (16) - Other columns is ID (Auto Trigger No), Command (Varchar2 = 256)
MY INLINE ASPX CODE:
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="COMMAND" HeaderText="COMMAND" SortExpression="COMMAND" />
<asp:TemplateField HeaderText="USERID">
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
DataTextField="USERNAME" DataValueField="USERID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
InsertCommand="INSERT INTO INSTRUCT (COMMAND, USERID) VALUES (:COMMAND, :USERID)">
<InsertParameters>
<asp:Parameter Name="COMMAND" Type="String" />
<asp:Parameter Name="USERID" DbType="Binary" Size="16" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT USERID, USERNAME FROM ORA_ASPNET_USERS">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
The above code will only insert the command textbox and but will not insert the USERID from the USERID dropdown.
I would really appreciate your help.
Thank you very much.
Replace your datasource <InsertParameter> with this one, it might help you, because I don't have your source code I cannot test it locally, the problem could occur somewhere elese though:
<InsertParameters>
<asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedValue" Name="USERID" Type="Byte"></asp:ControlParameter>
<asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedItem.Text" Name="COMMAND" Type="String"></asp:ControlParameter>
</InsertParameters>

Input string was not in a correct format vb.net/asp.net

Here's my formview..
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="recid">
<EditItemTemplate>
RECID:
<asp:TextBox ID="recid" runat="server" Text='<%# Eval("RECID") %>' ReadOnly="true" />
<br />
SHIPPER:
<asp:TextBox ID="shipper" runat="server" Text='<%# Bind("SHIPPER") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:FormView>
here's my sqldatasource1
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM IMPORT WHERE (RECID = ?)"
UpdateCommand="UPDATE IMPORT SET SHIPPER=#shipper WHERE RECID=#recid" >
<SelectParameters>
<asp:ControlParameter ControlID="Label1" Name="RECID" PropertyName="Text" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="recid" Type="Int32" />
<asp:Parameter Name="shipper" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
Now, my problem is that everytime I update the page, this is the error Input string was not in a correct format.
I tried changing the #recid in the UpdateCommand into a constant UpdateCommand="UPDATE IMPORT SET SHIPPER=#shipper WHERE RECID=11186" this worked well, the update was successful.
I think the #recid is being treated as a string here in the UpdateCommand. Can someone please help me? What do I need to do in order to change the data type of #recid to Integer?
Thanks!
First thing you need to fix is to bind raceid with the textbox like below:
<asp:TextBox ID="recid" runat="server" Text='<%# Bind("RECID") %>' ReadOnly="true" />
Now there's a question about provider. In select command you have used RACEID = ?, ? is used for OleDb or Odbc. If you are using any of them, you have to change Update Command to this:
UpdateCommand="UPDATE IMPORT SET SHIPPER=? WHERE RECID=?"
And UpdateParameters to this (Notice their order):
<UpdateParameters>
<asp:Parameter Name="shipper" Type="String" />
<asp:Parameter Name="recid" Type="Int32" />
</UpdateParameters>
I would recommend reading this MSDN article: Using Parameters with the SqlDataSource Control

Autogenerated Gridview with All Cells in Edit Mode by Default

So I've got a gridview bound to a SQL datasource stored procedure. The stored procedure returns a pivot table, where a number of columns are unknown until run time. I therefore need to autogenerate the columns.
The gridview is intended to allow Excel-like data updates. So the goal is to have the grid load as textboxes. I know if I were to declare the columns on my aspx page, I could create template columns and handle their visibility that way.
So I believe I need a way to programmatically set all the columns to template fields (without knowing the column names), or I need to discover the method by which I can just "flip the switch" and just make everything editable.
Thanks in advance!
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSourceExcelGridTest">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceExcelGridTest" runat="server" ConnectionString="<%$ ConnectionStrings:WebAppsConnectionString %>" SelectCommand="spJobForecastingGetEmployeesByDepartmentAndProject_v1" SelectCommandType="StoredProcedure" UpdateCommand="spJobForecastingUpdateHours" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="DepartmentNumber" Type="String" DefaultValue="13211" />
<asp:Parameter Name="ProjectNumber" Type="String" DefaultValue="13211" />
<asp:Parameter Name="startDate" Type="DateTime" DefaultValue="2/21/2014" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DepartmentNumber" Type="String" />
<asp:Parameter Name="ProjectNumber" Type="String" />
<asp:Parameter Name="Alias" Type="String" />
<asp:Parameter Name="WorkWeek" Type="DateTime" />
<asp:Parameter Name="WorkHours" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
try this:
UPDATED CODE
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSourceExcelGridTest">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceExcelGridTest" runat="server" ConnectionString="<%$ ConnectionStrings:WebAppsConnectionString %>" SelectCommand="spJobForecastingGetEmployeesByDepartmentAndProject_v1" SelectCommandType="StoredProcedure" UpdateCommand="spJobForecastingUpdateHours" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:Textbox ID="DepartmentNumber" DefaultValue="13211" text='<%# Eval("DepartmentNumber") %>'></asp:TextBox>
<asp:Textbox ID="ProjectNumber" Type="String" DefaultValue="13211" Text='<%# Eval("ProjectNumber") %>'></asp:TextBox>
<asp:Textbox ID="startDate" Type="DateTime" DefaultValue="2/21/2014" Text='<%# Eval("startDate") %>'></asp:TextBox>
</SelectParameters>
<UpdateParameters>
<asp:Textbox ID="DepartmentNumber" Type="String" text='<%# Eval("DepartmentNumber") %>'></asp:TextBox>
<asp:Textbox ID="ProjectNumber" Type="String" text='<%# Eval("ProjectNumber") %>'></asp:TextBox>
<asp:Textbox ID="Alias" Type="String" text='<%# Eval("Alias") %>'></asp:TextBox>
<asp:Textbox ID="WorkWeek" Type="DateTime" text='<%# Eval("WorkWeek") %>'></asp:TextBox>
<asp:Textbox ID="WorkHours" Type="Int32" text='<%# Eval("WorkHours") %>'></asp:TextBox>
</UpdateParameters>
</asp:SqlDataSource>

ASP VB - How to update a database where the selected item is from a dropdown list using a datasource

Hi I am new to ASP and was able to create a dropdown list which list all the items from my table (DB2_INSTANCES).
Now I need to update a second table (PROCESS) with the value selected DB2_DSN. The table does not allow Null values.
If I use "SelectedValue='<%# Bind("DB2_DSN") %>' within my asp:DropDownList after I set the
DataSourceID="SqlDataSource6" DataTextField="INSTANCE" DataValueField="INSTANCE"
I get an error:
"'DropDownList6' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value "
If I change to "SelectedValue='<%# Bind("INSTANCE") %>' within my asp:DropDownList after I set the
DataSourceID="SqlDataSource6" DataTextField="INSTANCE" DataValueField="INSTANCE"
I get an error:
"DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'INSTANCE'."
If I remove the "SelectedValue..." I get the error:
Cannot insert the value NULL into column 'DB2_DSN', table 'xxx.dbo.PROCESS'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
I also tried using the DataBind() but it also gives me an error.
The other value (FTP_IND) works fine because it is listed/hardcoded.
Why does it work for FTP_IND and not for DB2_DSN?
First:
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxConnectionString2 %>"
DeleteCommand="DELETE FROM PROCESS WHERE NAME = #NAME AND ENVIRONMENT = #ENVIRONMENT"
SelectCommand="SELECT * FROM [PROCESS]"
UpdateCommand="UPDATE PROCESS SET ENVIRONMENT = #ENVIRONMENT, PROCESS_STATUS = #PROCESS_STATUS, IC_VALUE = #IC_VALUE,
FTP_IND = #FTP_IND, DB2_DSN = #DB2_DSN, DB2_REGION = #DB2_REGION,
ALERT_EMAIL = #ALERT_EMAIL, NOTIFY_EMAIL = #NOTIFY_EMAIL, DEBUG_INFO = #DEBUG_INFO,
LAST_UPDATE_DATE = CURRENT_TIMESTAMP, LAST_UPDATE_USERID = #UID, LAST_UPDATE_IP = #UIP WHERE (NAME = #NAME)">
<UpdateParameters>
<asp:SessionParameter Name="UID" SessionField="User_ID" Type="String" />
<asp:SessionParameter Name="UIP" SessionField="User_IP" Type="String" />
<asp:Parameter Name="ENVIRONMENT" />
<asp:Parameter Name="PROCESS_STATUS" />
<asp:Parameter Name="IC_VALUE" />
<asp:Parameter Name="FTP_IND" />
<asp:Parameter Name="DB2_DSN" />
<asp:Parameter Name="DB2_REGION" />
<asp:Parameter Name="ALERT_EMAIL" />
<asp:Parameter Name="NOTIFY_EMAIL" />
<asp:Parameter Name="DEBUG_INFO" />
<asp:Parameter Name="NAME" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource6" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxConnectionString2 %>"
SelectCommand="SELECT [INSTANCE] FROM [DB2_INSTANCES] ORDER BY [INSTANCE] ASC">
</asp:SqlDataSource>
Second:
<asp:TemplateField HeaderText="FTP" SortExpression="FTP_IND">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server"
SelectedValue='<%# Bind("FTP_IND") %>'
ToolTip="Use this to turn off and on the FTP process" Font-Size="Small">
<asp:ListItem>N</asp:ListItem>
<asp:ListItem>Y</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("FTP_IND") %>'
ToolTip="Indicates if FTP will take place" Font-Size="Small">
</asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="Small" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText = "DB2 Instance" SortExpression="DB2_DSN">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server"
ToolTip="Use this to choose the DB2 Instance" Font-Size="Small"
DataSourceID="SqlDataSource6"
DataTextField="INSTANCE"
DataValueField="INSTANCE">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("DB2_DSN") %>'
ToolTip="Indicates the DB2 Instance being used" Font-Size="Small">
</asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="Small" HorizontalAlign="Center" />

ASP.NET error, must declare the scalar variable "#ServerName" after second dropdownlist is select

There are 3 dropdownlists which are parent-child, after the 3rd dropdownlist is selected, the DetailsView will display result from all 3 dropdownlist selected. first is ServerName, second is Instance, then third is DatabaseName. After select first dropdownlist of servername, then new list of instance value appear on second dropdownlist. When I select anything on second dropdownlist. There a error message that say, "Must declare the scalar variable "#ServerName"". I don't understand what it mean and please help. Here a dropdownlist codes,
<ajaxToolkit:ComboBox ID="ComboBox1" runat="server" AutoCompleteMode="SuggestAppend" AutoPostBack="True" DataSourceID="SqlDataSource4" DataTextField="ServerName" DataValueField="ServerName" DropDownStyle="Simple" MaxLength="0" style="display: inline;">
</ajaxToolkit:ComboBox>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [ServerName] FROM [tblServer]">
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
</asp:DropDownList>
<asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = #ServerName">
<SelectParameters>
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = #ServerName AND [Instance] = #Instance">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Then I pass those dropdownlist function to DetailsView that come with SQLDataSource2,
on SelectCommand in SQLDataSource2, I wrote
SelectCommand="SELECT * FROM [tblDatabase] WHERE (([DatabaseName] = #DatabaseName) AND ([Instance] = #Instance) AND ([ServerName] = #ServerName))"
Then after that I add SelectParameter codes,
<SelectParameters>
<asp:ControlParameter ControlID="DropDownDatabase" Name="DatabaseName" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
In SqlDataSource3, your query takes two parameters: #ServerName and #Instance. But in the select parameters, you only define #Instance. Yes, you defined #ServerName in the previous data source, but not in this one.
I fixed from what Jay metioned recently and there also another issue that I fix it on instance and database dropdownlist codes, where what I fix and it work fine.
<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
</asp:DropDownList>
<asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = #ServerName">
<SelectParameters>
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = #ServerName AND [Instance] = #Instance">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

Resources