Pass textbox User input from Dot Net Form to Database and Display result from database to form

I had created a user interface form which will accept two inputs from Player A and B and when they click on Play button subsequent inputs will be sent to database and result will be shown in form. Below are steps for creating user interactive form

In the Controller Class – ADD Method

public IActionResult DataTBRules()
{

        return View();
    }

Add view in order to create a display user interface form

Give View name same as Action name

User Interface Form in view Play Rummy

Write following code in the view form

@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{<p> PLAY RUMMY 2 PLAYERS</p>
 <img src="~/images/banner2.svg" alt="Visual Studio" class="img-responsive" />
    <hr /><p> Player A </p>
    <input type="text" name="IP" id="IP" />
    <hr />
    <p> Player B </p>
    <input type="text" name="Name" id="Name" />
    <hr />
    <p> Play  </p>
    <input type="submit" value="Login" />
}

Display will look like this

User Input to database

These are the records in my table

How to fetch data from database with button action Play Rummy Code

Write this in view form DataTBRules.cshtml

@using (Html.BeginForm(“Login“, “Home“, FormMethod.Post))

Login – Action method name which will be called when button hits

Home – Controller Class name

Below code should be written in button action method

In this method when user gives input in 2 textboxes

Player A and Player B and click on button, this method will be called

Player A input and Player B input will be passed as S1 and S2

[HttpPost]
public String Login(string PlayerA , string PlayerB)
{
string s1 = PlayerA;
string s2 = PlayerB;
SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
csb.DataSource = “ServerName”;
csb.InitialCatalog = “master”;
csb.IntegratedSecurity = true;
csb.UserID = “UserID”;
csb.Password = “Password”;

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = csb.ToString();
        
        SqlCommand sqlcmd = conn.CreateCommand();
        try
        {
            conn.Open();
           
             sqlcmd = conn.CreateCommand();
            sqlcmd.Connection = conn;
            sqlcmd.CommandType = System.Data.CommandType.Text;
            sqlcmd.CommandText = "select InputA,InputB,Winner from TB_Rules where InputA = '" + s1  + "' and  InputB = '" + s2 + "'";

            SqlDataAdapter sdar = new SqlDataAdapter(sqlcmd);
            

            SqlDataReader sqdaar = sqlcmd.ExecuteReader();
            int numrws = 0;
            bool rcvcnt = sqdaar.HasRows;
            string reswin = null;
            // print the Values of each record
            while (sqdaar.Read())
            {
                numrws = numrws + 1;                
                reswin = sqdaar["Winner"].ToString();
            }
           
             if (reswin == s1)
                {
                    reswin = "Player A Won";
                }
                else if (reswin == s2)
                {
                    reswin = "Player A Won";
                }
                else
                {
                    reswin = "Round DRAW";
                }   
            return " Result " + reswin;

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return  "Error connected to  SQl Server Database as " + ex.Message ;
        }

        }

Run the Application in IIS Server and pass Values

Click on Play Button

Result Round DRAW

Feel Free to ask any questions in case the code doesn’t work. Thanks for Reading

Leave a Reply

Your email address will not be published. Required fields are marked *