hi all... i have create a registration form using Form View tool in asp.net..and i have a drop down list in form..
i use dropdown list in my cs page but i can't get the data into my drop down list in Form View Insert Template..(but no error display)
i use this code to get data to drop down list..
protected void Page_Load(object sender, Event Args e)
{
DropDownList ddlFreigh = (DropDownList)(FormView1.FindControl("ddlFreigh"));
if (!IsPostBack)
{
// Create a database variable
SqlConnection connection = new SqlConnection();
// Set the connection string of the database
connection.ConnectionString = ConfigurationManager.ConnectionStrings[databaseConnectionStringName].ToString();
try
{
connection.Open();
#region Loading freigh to drop down list
SqlCommand freighCommand = connection.CreateCommand();
freighCommand.CommandText = "SELECT Value FROM UDFList WHERE Name='FREIGH'";
freighCommand.CommandType = CommandType.Text;
string freighString = freighCommand.ExecuteScalar().ToString();
string[] listOfFreigh = freighString.Split();
ddlFreigh.Items.Clear();
foreach (string freigh in listOfFreigh)
{
ddlFreigh.Items.Add(freigh);
}
freighCommand.Dispose();
#endregion
}
catch
{ }
finally
{
try
{
connection.Close();
}
catch { }
}
}
Rakesh JhaPosted Oct 4, 2010, 2:17 AM
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1">
<EditItemTemplate>
fname:
<asp:TextBox ID="fnameTextBox" runat="server" Text='<%# Bind("fname") %>' />
<br />
lname:
<asp:TextBox ID="lnameTextBox" runat="server" Text='<%# Bind("lname") %>' />
<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>
<InsertItemTemplate>
fname:
<asp:TextBox ID="fnameTextBox" runat="server" Text='<%# Bind("fname") %>' />
<br />
lname:
<asp:TextBox ID="lnameTextBox" runat="server" Text='<%# Bind("lname") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
InsertItemTemplate>
<ItemTemplate>
fname:
<asp:Label ID="fnameLabel" runat="server" Text='<%# Bind("fname") %>' />
<br />
lname:
<asp:Label ID="lnameLabel" runat="server" Text='<%# Bind("lname") %>' />
<br />
sname
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Value"
DataValueField="Value" >
asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:databaseConnectionStringName %>"
SelectCommand="SELECT Value FROM UDFList WHERE Name='FREIGH' order by value">
asp:SqlDataSource>
<br />
ItemTemplate>
asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:databaseConnectionStringName %>"
SelectCommand="select fname,lname from dir_enrol where dirid=@dirid">
<SelectParameters>
<asp:ControlParameter ControlID="FormView1" DefaultValue="1000376" Name="dirid"
PropertyName="SelectedValue" />
SelectParameters>
asp:SqlDataSource>
jeeva sanPosted Oct 3, 2010, 10:15 PM
i have follow your codes and i add (onDataBound="FormView1_DataBound") on my aspx page..
but i did't get any result...no errors display.
Rakesh JhaPosted Oct 3, 2010, 6:28 AM
protected void FormView1_DataBound(object sender, EventArgs e)
{
DropDownList ddlFreigh = (DropDownList)(FormView1.FindControl("ddlFreigh"));
if (!IsPostBack)
{
// Create a database variable
SqlConnection connection = new SqlConnection();
// Set the connection string of the database
connection.ConnectionString = ConfigurationManager.ConnectionStrings[databaseConnectionStringName].ToString();
try
{
connection.Open();
#region Loading freigh to drop down list
SqlCommand freighCommand = connection.CreateCommand();
freighCommand.CommandText = "SELECT Value FROM UDFList WHERE Name='FREIGH'";
freighCommand.CommandType = CommandType.Text;
string freighString = freighCommand.ExecuteScalar().ToString();
string[] listOfFreigh = freighString.Split();
ddlFreigh.Items.Clear();
foreach (string freigh in listOfFreigh)
{
ddlFreigh.Items.Add(freigh);
}
freighCommand.Dispose();
#endregion
}
catch
{ }
finally
{
try
{
connection.Close();
}
catch { }
}
}
}