locked
Regarding C# RRS feed

  • Question

  • User-1162083151 posted

    Dear Sir,

    I want to start coding in C# thats y I try to create new form in C# Coding,can anyone give me one example or task to create new form.

    Regards,

    Nikhil

    Saturday, October 5, 2019 10:31 AM

Answers

  • User665608656 posted

    Hi nsawant,

    According to your description, What 'form' do you refer to, winform or webform?

    If it is webform , I created a simple to add data to the database, you can follow these codes:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <table class="auto-style1">
                    <tr>
                        <td>Name :</td>
                        <td>
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        </td> 
                    </tr>
                    <tr>
                        <td>Subject :</td>
                        <td>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                                <asp:ListItem Text="English" Value="English" Selected="True"></asp:ListItem>
                                <asp:ListItem Text="Maths" Value="Maths"></asp:ListItem>
                                <asp:ListItem Text="PE" Value="PE"></asp:ListItem>
                                <asp:ListItem Text="Music" Value="Music"></asp:ListItem>
                            </asp:DropDownList>
                        
                        </td>
                    </tr>
                    <tr>
                        <td>Score :</td>
                        <td>
                            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
                        </td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
    </html>
     protected void Button1_Click(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                string sql = "INSERT INTO Students (SName,Subject,Score) VALUES (@SName,@Subject,@Score)";
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@SName", TextBox1.Text.ToString());
                cmd.Parameters.AddWithValue("@Subject", DropDownList1.SelectedValue.ToString());
                cmd.Parameters.AddWithValue("@Score", Convert.ToInt32(TextBox3.Text.ToString()));
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                con.Close();
            }

    For more details, you could refer to this link : Creating A Simple Registration Form In ASP.NET

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2019 8:32 AM