Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that has two dropdownlists that are databound. The first ddl is school names and the other has a year in it. What I am trying to do is to populate the databound ddl with just the data that the User ID matches. Also, is there a way to just have one year in the ddl that when selected it will populate the textboxes I have? What I mean is that the year ddl is databound and it has 2012 in it for 200 users. Is there a way I can just have one 2012 in that ddl and it goes by the User ID that the ddl school does?

C#
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing.Printing;
using System.Web.SessionState;

public partial class FinancialProfileFormC : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ButtonPrint.Attributes.Add("onclick", "window.print(); return false");
    }
    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con.Open();



        SqlCommand cmd = new SqlCommand("Insert into TableFIN2013 (INST_ID, TOTAL_REVE, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, NET_AID, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LOMGTERMDEBT) values (@INST_ID, @TOTAL_REVE, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @NET_AID, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LOMGTERMDEBT)", con);

        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTRIR.Text);
        cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxInstr.Text);
        cmd.Parameters.AddWithValue("@RESEARCH", TextBoxRes.Text);
        cmd.Parameters.AddWithValue("@PUBLIC_SER", TextBoxPubS.Text);
        cmd.Parameters.AddWithValue("@ACADEMIC_S", TextBoxAcad.Text);
        cmd.Parameters.AddWithValue("@STUDENT_SE", TextBoxStudS.Text);
        cmd.Parameters.AddWithValue("@INSTITUTIO", TextBoxInstiS.Text);
        cmd.Parameters.AddWithValue("@PHYSICAL_P", TextBoxOperM.Text);
        cmd.Parameters.AddWithValue("@NET_AID", TextBoxNGAS.Text);
        cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text);
        cmd.Parameters.AddWithValue("@HOSPITALS", TextBoxHosS.Text);
        cmd.Parameters.AddWithValue("@INDEPENDEN", TextBoxIndeO.Text);
        cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxOE.Text);
        cmd.Parameters.AddWithValue("@TOTASSETS", TextBoxTA.Text);
        cmd.Parameters.AddWithValue("@TOTLIABILITY", TextBoxTL.Text);
        cmd.Parameters.AddWithValue("@NoNEXPPERMRESASSETS", TextBoxNPRNA.Text);
        cmd.Parameters.AddWithValue("@EXPENDABLE", TextBoxETRNA.Text);
        cmd.Parameters.AddWithValue("@UNRNETASSETS", TextBoxTUNA.Text);
        cmd.Parameters.AddWithValue("@TOTALREV", TextBoxTR.Text);
        cmd.Parameters.AddWithValue("@TUITFEES", TextBoxTFN.Text);
        cmd.Parameters.AddWithValue("@CURRDEBT", TextBoxCD.Text);
        cmd.Parameters.AddWithValue("@LOMGTERMDEBT", TextBoxLTD.Text);

        con.Open();
        cmd.ExecuteNonQuery();
    }
    protected void ButtonPrint_Click(object sender, EventArgs e)
    {

    }

    protected void DropDownListSchool_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con.Open();

        SqlCommand scmd = new SqlCommand("Select INST_ID from TableCOCINST where INST_ID = " + DropDownListSchool.SelectedValue.ToString(), con);
        SqlDataReader dr = scmd.ExecuteReader();
        if (dr.Read())
        {
            
            TextBoxINST_ID.Text = dr["INST_ID"].ToString();
        }
        

        dr.Close();
        con.Close(); 
   }
    protected void DropDownListLY_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
       con.Open();

       SqlCommand scmd = new SqlCommand("Select INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, NET_AID, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT from TableFIN2012 where INST_ID = " + DropDownListSchool.SelectedValue.ToString(), con);
       SqlDataReader dr = scmd.ExecuteReader();
       if (dr.Read())
       {
           TextBoxLYInstr.Text = dr["INSTRUCTIO"].ToString();
           TextBoxLYRes.Text = dr["RESEARCH"].ToString();
           TextBoxLYPubS.Text = dr["PUBLIC_SER"].ToString();
           TextBoxLYAcad.Text = dr["ACADEMIC_S"].ToString();
           TextBoxLYStudS.Text = dr["STUDENT_SE"].ToString();
           TextBoxLYInstiS.Text = dr["INSTITUTIO"].ToString();
           TextBoxLYOperM.Text = dr["PHYSICAL_P"].ToString();
           TextBoxLYNGAS.Text = dr["NET_AID"].ToString();
           TextBoxLYAuxE.Text = dr["AUXILIARY_"].ToString();
           TextBoxLYHosS.Text = dr["HOSPITALS"].ToString();
           TextBoxLYIndeO.Text = dr["INDEPENDEN"].ToString();
           TextBoxLYOE.Text = dr["OTHEREXP"].ToString();
           TextBoxLYTA.Text = dr["TOTASSETS"].ToString();
           TextBoxLYTL.Text = dr["TOTLIABILITY"].ToString();
           TextBoxLYNPRNA.Text = dr["NoNEXPPERMRESASSETS"].ToString();
           TextBoxLYTUNA.Text = dr["UNRNETASSETS"].ToString();
           TextBoxLYTR.Text = dr["TOTALREV"].ToString();
           TextBoxLYTFN.Text = dr["TUITFEES"].ToString();
           TextBoxLYCD.Text = dr["CURRDEBT"].ToString();
           TextBoxLYLTD.Text = dr["LONGTERMDEBT"].ToString();

       
           
           
       }
       dr.Close();
       con.Close();

    }
}

ASPX CODE:

Ok, sorry. I had the wrong code. I didn't read the question carefully. Here is the code I think you need. 
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:PasswordConnectionString %>" 
                    SelectCommand="SELECT [INST_ID], [LongName] FROM [TableCOCINST]">
                </asp:SqlDataSource>
Posted
Updated 3-Oct-13 8:40am
v3
Comments
ZurdoDev 3-Oct-13 13:30pm    
I'm confused. Where exactly are you stuck?
Not clear.
Computer Wiz99 3-Oct-13 13:47pm    
Ok. The ddl has all the schools in the database to show in the ddl and not just the school that has the same ID as the user. User ID is 1234 then I want the school that has ID 1234 to just populate into the ddl. Not all of them. So when I select the school for that user it will populate the textboxes with the date from that school and not all of them.
You have not posted the code which Pilates the values in ddl? Please post that. You just need to add one condition to that query which fetches values for the ddl? Do you get me?
Computer Wiz99 3-Oct-13 14:06pm    
The only values I have for the ddl is the ID for the school and year. I setup the databound in the Data Source Configuration Wizard.

1 solution

If you want to add one Session condition to DropDownList "DropDownListSchool", then do like below...
XML
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:PasswordConnectionString %>"
            SelectCommand="SELECT * FROM [TableCOCINST] WHERE UserID = ?"


Now add one Select Parameter like below...
XML
<SelectParameters>
  <asp:SessionParameter
    Name="UserID"
    SessionField="UserID"
    DefaultValue="5" />
</SelectParameters>


Note- I don't know the Table structure, so I just assume that you have a UserID in this table. You need to modify that.
But this is trick to add one Session parameter condition.

Reference- SessionParameter Class[^]
 
Share this answer
 
Comments
Computer Wiz99 4-Oct-13 8:05am    
Tadit Dash, Thanks for the info. I have a question, I have to put this in on every form page in aspx right?
Yes, where ever you have the Grid and SqlDataSource.
Computer Wiz99 4-Oct-13 8:14am    
Ok. I'm not sure I follow the Select Parameters part. Name, SessionField and DefaultValue = 5. Name would be the User ID that is in the database, SessionField would be the User ID in the Database, DefaultValue = 5? What is that and what does the 5 mean?
I don't know why it has written "?" in the MSDN link, there may be some issue.

Use like below... Refer- Passing Session parameter into SQLdatasource[^]
and modify the SelectCommand Like below...

SelectCommand="SELECT * FROM [TableCOCINST] WHERE UserID = @UserID"

Now, I am explaining the SelectParameter...

<SelectParameters>
<asp:SessionParameter
Name="UserID" --> Name of the Parameter in SelectCommand that is @UserID
SessionField="UserID" --> Session Field Name
DefaultValue="5" /> --> If no value is there in Session, this will be the default.
</SelectParameters>


Do you understand now? If any doubt, feel free to ask.
Computer Wiz99 4-Oct-13 8:41am    
Ok. I am getting an error where SelectParameters are at. Error is saying that: Validation (XHTML 1.0 Transitional): Element 'selectparameters' is not supported. Why is that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900