basically, i have a dropdownlist ddlSearch. The user can select a project from the dropdown list. The list is populated from a table in the projects database
I want these fields to be populated depending on which ProjectName the user chooses from the ddlSearch dropdownlist. Im not sure how to get those fields to populate after making a selection as i am very new. can anyone assist with coding help in C#?
also im not wanting to bing this to the gridview. what i am trying to do is get the select statement to populate the textfields and checkboxes..i included the variables and the field names in my initial post.
Should it say DropDownList2_SelectedIndexChanged? or ddlSearch_SelectedIndexChanged?
If you let the VS generates the event for you then basically it will generate like "ddlSearch_SelectedIndexChanged"..As you noticed It will just append the ID of the control to the event SelectedIndexChange.. But in that case you have mentioned it does
not matter if you use DropDownList2_SelectedIndexChanged or ddlSearch_SelectedIndexChanged for as long as it will match the event handler in your code behind.. see below
also im not wanting to bing this to the gridview. what i am trying to do is get the select statement to populate the textfields and checkboxes..i included the variables and the field names in my initial post.
What about this
private void FillTextBoxes(string ProjectName)
{
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
string sql = "select * from TableName where ProjectName='"+ProjectName+"'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
TextBox1.Text= dt.Rows[0]["ColumnName"].TosTring(); //Where column name us the Fields for your Table that you wanted to display in the TextBoxes
TextBox2.Text= dt.Rows[0]["ColumnName"].TosTring();
}
conn.Close();
awesome. but what if you have a radiobuttonlist? how do you populate that from the database? rblStatus
Same steps
private void FillRBL(string ProjectName)
{
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
string sql = "select * from TableName where ProjectName='"+ProjectName+"'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
RadioButtonList1.DataSource = dt;
RadioButtonList1.DataTextField = "ColumnName"; // DataTextField is where the Fields are being displayed in RBL Text
RadioButtonList1.DataValueFieild = "ColumnName"; //DataValueFieild usually contains the ID of the fields
RadioButtonList1.DataBind();
}
conn.Close();
kcrewjap
Member
15 Points
188 Posts
Populate fields from dropdown selection
Jun 18, 2008 09:04 PM|LINK
Hello All,
I have a page that i built that the user can enter a project and its associated features and descriptions in. The parameters are below:
sqlCmd.Parameters.Add("@dtDate", SqlDbType.DateTime).Value = strDate;sqlCmd.Parameters.Add(
"@nvchNT", SqlDbType.NVarChar).Value = strNTUser; sqlCmd.Parameters.Add("@nvchProjectNm", SqlDbType.NVarChar).Value = strProjectName;sqlCmd.Parameters.Add(
"@nvchProjectDesc", SqlDbType.NVarChar).Value = strProjectDesc; sqlCmd.Parameters.Add("@intOccurrenceId", SqlDbType.NVarChar).Value = strOccurrenceId;sqlCmd.Parameters.Add(
"@nvchOccurrence", SqlDbType.NVarChar).Value = strOccurrence; sqlCmd.Parameters.Add("@btAutomated", SqlDbType.NVarChar).Value = bAutomated;sqlCmd.Parameters.Add(
"@btManual", SqlDbType.NVarChar).Value = bManual; sqlCmd.Parameters.Add("@intStatusId", SqlDbType.Bit).Value = strStatusId;sqlCmd.Parameters.Add(
"@nvchStatusName", SqlDbType.NVarChar).Value = strStatusName; sqlCmd.Parameters.Add("@nvchAssignedMembers", SqlDbType.NVarChar).Value = strAssignedMembers;sqlCmd.Parameters.Add(
"@dtCompDate", SqlDbType.DateTime).Value = strCompDate;basically, i have a dropdownlist ddlSearch. The user can select a project from the dropdown list. The list is populated from a table in the projects database
the columns are:
RequestDate(datetime), NTLogin, ProjectName, ProjectDesc, OccurenceId(int), Occurrence, Automated(bit), Manual(bit), statusId(int), StatusName, AssignedMembers, CompletionDate(datetime)
I want these fields to be populated depending on which ProjectName the user chooses from the ddlSearch dropdownlist. Im not sure how to get those fields to populate after making a selection as i am very new. can anyone assist with coding help in C#?
Hua-Jun Li -...
All-Star
75950 Points
5608 Posts
Re: Populate fields from dropdown selection
Jun 20, 2008 03:56 AM|LINK
Hi kcrewjap,
You can try the following method.
<asp:DropDownList ID="ddlSearch " runat="server" Height="21px" Width="74px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
Please set property AutoPostBack="True" for server control dropdownlist.
Then you can add the event SelectedIndexChanged handler function.
protected void ddlSearch_SelectedIndexChanged(object sender, EventArgs e)
{
string ProjectName = DropDownList1.SelectedValue;
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
string sql = "select * from TableName where ProjectName='"+ProjectName+"'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dgProject.DataSource = ds;
dgProject.DataBind();
conn.Close();
}
Then you can bind the data with GridView, it wil ok.
Let me know if I have misunderstood what you mean.
Thanks.
Hope it helps,
Hua Jun
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
kcrewjap
Member
15 Points
188 Posts
Re: Populate fields from dropdown selection
Jun 20, 2008 04:34 PM|LINK
Thank you for your reply. Just a couple questions.
1. <asp:DropDownList ID="ddlSearch " runat="server" Height="21px" Width="74px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
Should it say DropDownList2_SelectedIndexChanged? or ddlSearch_SelectedIndexChanged?
2. DataSet ds = new DataSet();
da.Fill(ds);
dgProject.DataSource = ds;
dgProject.DataBind();
conn.Close();
where is the dgProject comming from?
Thanks you for your help
kcrewjap
Member
15 Points
188 Posts
Re: Populate fields from dropdown selection
Jun 20, 2008 04:50 PM|LINK
also im not wanting to bing this to the gridview. what i am trying to do is get the select statement to populate the textfields and checkboxes..i included the variables and the field names in my initial post.
thanks
vinz
All-Star
127011 Points
17934 Posts
MVP
Re: Populate fields from dropdown selection
Jun 20, 2008 04:54 PM|LINK
If you let the VS generates the event for you then basically it will generate like "ddlSearch_SelectedIndexChanged"..As you noticed It will just append the ID of the control to the event SelectedIndexChange.. But in that case you have mentioned it does not matter if you use DropDownList2_SelectedIndexChanged or ddlSearch_SelectedIndexChanged for as long as it will match the event handler in your code behind.. see below
<asp:DropDownList ID="ddlSearch " runat="server" Height="21px" Width="74px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="True">
if using the above declaration then event handler method should look like this below
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
}
Or you declare it this way
<asp:DropDownList ID="ddlSearch " runat="server" Height="21px" Width="74px" OnSelectedIndexChanged="ddlSearch _SelectedIndexChanged" AutoPostBack="True">
Then your event handler method should look like this below
protected void ddlSearch_SelectedIndexChanged(object sender, EventArgs e)
{
}
Hope you get it..
dgProject is the ID of your Grid control that you are using..
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
vinz
All-Star
127011 Points
17934 Posts
MVP
Re: Populate fields from dropdown selection
Jun 20, 2008 05:00 PM|LINK
What about this
private void FillTextBoxes(string ProjectName)
{
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
string sql = "select * from TableName where ProjectName='"+ProjectName+"'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
TextBox1.Text= dt.Rows[0]["ColumnName"].TosTring(); //Where column name us the Fields for your Table that you wanted to display in the TextBoxes
TextBox2.Text= dt.Rows[0]["ColumnName"].TosTring();
}
conn.Close();
}
protected void ddlSearch_SelectedIndexChanged(object sender, EventArgs e)
{
FillTextBoxes(DropDownList1.SelectedValue);
}
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
kcrewjap
Member
15 Points
188 Posts
Re: Populate fields from dropdown selection
Jun 20, 2008 05:28 PM|LINK
awesome. but what if you have a radiobuttonlist? how do you populate that from the database? rblStatus
vinz
All-Star
127011 Points
17934 Posts
MVP
Re: Populate fields from dropdown selection
Jun 20, 2008 05:48 PM|LINK
Same steps
private void FillRBL(string ProjectName)
{
SqlConnection conn = new SqlConnection(strCon);
conn.Open();
string sql = "select * from TableName where ProjectName='"+ProjectName+"'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
RadioButtonList1.DataSource = dt;
RadioButtonList1.DataTextField = "ColumnName"; // DataTextField is where the Fields are being displayed in RBL Text
RadioButtonList1.DataValueFieild = "ColumnName"; //DataValueFieild usually contains the ID of the fields
RadioButtonList1.DataBind();
}
conn.Close();
}
protected void ddlSearch_SelectedIndexChanged(object sender, EventArgs e)
{
FillRBL(DropDownList1.SelectedValue);
}
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
kcrewjap
Member
15 Points
188 Posts
Re: Populate fields from dropdown selection
Jun 20, 2008 08:42 PM|LINK
and for a dropdownlist
i haveddlOccurrence.SelectedValue = dt.Rows[0][
"Occurrence"].ToString();however it doesnt like the SelectedValue nor SelectedIndex.. which option to i put for a ddl?
Thanks for all of your time and help
vinz
All-Star
127011 Points
17934 Posts
MVP
Re: Populate fields from dropdown selection
Jun 20, 2008 08:47 PM|LINK
If you wanted to add Items in your DropDownList then have it this way
string strItem = dt.Rows[0]["Occurrence"].ToString();
ddlOccurrence.Items.Add(strItem);
MessageBox Controls for WebForms | Blog | Twitter | Linkedin