Avatar of Prncesnicki
Prncesnicki
Flag for United States of America asked on

Is there a way to create a form on a button click using C# and ASP.NET?

I have a page that contains a ddl of employees from an Access DB.  I have one button that grabs data from a table when an employee is selected in the ddl and the button is clicked.  I want to create another button that brings up the 3 text boxes and one ddl with choices from the the table.  I also want the one text box to autopopulate the ID for the selected employee and the other to autopopulate the year. Once info in selected and added, I want a submit button to update the Access DB.  Is this possible or am I out of my mind?
Microsoft Access.NET ProgrammingC#

Avatar of undefined
Last Comment
Prncesnicki

8/22/2022 - Mon
Anurag Thakur

dont create new controls (which you can do dynamivally) but instead the controls are already there but just their Visible is set to False and when the button click happens turn the visible state to true

or use a span and just change the visible state of the span from false to true and vice versa
Prncesnicki

ASKER
Can you give me a code example of a simple text box like this?
Prncesnicki

ASKER
I created everything and set them to invisible.  Can you please help me with the code for clicking the button to turn these to visible?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Anurag Thakur

you have a text box on the aspx page called txtName
set Visible="false" on the aspx page

when the button click happens and now you want to make the text box visible jsut add txtName.Visible = true;
Prncesnicki

ASKER
Actually the real problem I am having here now is figuring out how to get the button to turn the boxes visible and link the SSN text box to the selected employee in the ddl.  Any clue on how to do that?
Prncesnicki

ASKER
This is all I have...
protected void New1(object sender, EventArgs e)
    {
        String strConn3 = "Provider=Microsoft.Jet.OLEDB.4.0; "
               + "Data Source=" + Request.ServerVariables["APPL_PHYSICAL_PATH"]
               + "/Database/Company.mdb";
 
        String sqlSelect = "Select * "
                                  + "from Performance_Review "
                                  + "INNER JOIN Employee "
                                  + "ON Performance_Review.ee_ID = Employee.ssn "
                                  + "WHERE Employee.ssn="
                                  + ddlEmp.Text;
    }          

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
amar31282

Here is your code for showing the textboxes with the data according to the selected value in the DropDownList
 <div>
        <asp:DropDownList ID="ddlEmp" runat="server">
            <asp:ListItem>Item1</asp:ListItem>
            <asp:ListItem>Item2</asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
        <asp:TextBox ID="TextBox1" runat="server" Visible="false"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server" Visible="false"></asp:TextBox>
    </div>  
 
 
 
 protected void Button1_Click(object sender, EventArgs e)
    {
        String strConn3 = "Provider=Microsoft.Jet.OLEDB.4.0; "
       + "Data Source=" + Request.ServerVariables["APPL_PHYSICAL_PATH"]
       + "/Database/Company.mdb";
        SqlConnection con = new SqlConnection(strConn3);
        String sqlSelect = "Select * "
                                  + "from Performance_Review "
                                  + "INNER JOIN Employee "
                                  + "ON Performance_Review.ee_ID = Employee.ssn "
                                  + "WHERE Employee.ssn="
                                  + ddlEmp.SelectedItem.Text;
        SqlDataAdapter da = new SqlDataAdapter(sqlSelect, con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            TextBox1.Text = dt.Rows[0]["ee_ID"].ToString();
            TextBox2.Text = dt.Rows[0]["ee_year"].ToString();
            TextBox1.Visible = true;
            TextBox2.Visible = true;
        }
        else
        {
            TextBox1.Visible = false;
            TextBox2.Visible = false;
        }
    }

Open in new window

Prncesnicki

ASKER
I kept getting an error on the "SqlDataAdapter" code so I modified your code a bit and now I can view the page however when I select an employee and click button I get the following error:

 No value given for one or more required parameters.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: No value given for one or more required parameters.

protected void Button1_Click(object sender, EventArgs e)
    
   {
        String strConn3 = "Provider=Microsoft.Jet.OLEDB.4.0; "
               + "Data Source=" + Request.ServerVariables["APPL_PHYSICAL_PATH"]
               + "/Database/Company.mdb";
 
        
        String sqlSelect = "Select * "
                                  + "from Performance_Review "
                                  + "INNER JOIN Employee "
                                  + "ON Performance_Review.ee_ID = Employee.ssn "
                                  + "WHERE Employee.ssn="
                                  + ddlEmp.SelectedItem.Text;
        
        OleDbConnection objConn3 = new OleDbConnection(strConn3);
        OleDbCommand objCommand3 = new OleDbCommand(sqlSelect, objConn3); 
        OleDbDataAdapter objAdapter3 = new OleDbDataAdapter(objCommand3);
 
        DataSet objDataSet = new DataSet();
 
          
        DataTable dt = new DataTable();
        objAdapter3.Fill(objDataSet, "dt");
        
        if (dt.Rows.Count > 0)
        {
            eeID.Text = dt.Rows[0]["ee_ID"].ToString();
            evalyr.Text = dt.Rows[1]["evaluation_year"].ToString();
            eeID.Visible = true;
            evalyr.Visible = true;
        }
        else
        {
            eeID.Visible = false;
            evalyr.Visible = false;
         }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Prncesnicki

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.