0

I'm pretty proficient in PHP, however been asked to use asp.net to design a web system.

Issue is in PHP, I know how to post values from a HTML Form and extract values from a DB. I want to do the same within asp.net however it seems the forms are processed in a different way. The following is the code I have.

<%@ Page Language="C#" AutoEventWireup="true"  %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
<asp:DropDownList ID="Type" runat="server">
    <asp:ListItem Selected="True">Select Template:</asp:ListItem>
    <asp:ListItem Value="1">Bike Template </asp:ListItem>
    <asp:ListItem Value="2">Cars Template</asp:ListItem>
</asp:DropDownList>
    </div>
        <p><asp:Button ID="Button1" runat="server" Text="OK" /></p>

</form>
    <textarea name="'emailmessage" rows="20" cols="100"/> </textarea>

       
</body>
</html>

I want to pass the value of the List Item IE. 1. (Bike Template) to the DB, which will then connect run a sp and input the text from the DB in the TextArea.

I don't need to actual code for this but just guidance on how to:

a) connect to the DB. b) on click of the button to run the sp and input the value of the field in the text area. Do I use something like:

protected void Button1_Click(object sender, EventArgs e) {  }?

Is there any post processing needed?

skippy
  • 172
  • 4
  • 17

1 Answers1

0

a) connect to the DB

If you are going to make a lot of calls to the database and working with typed object, the most common way to interact with the database is Entity Framework. The first steps in EF can be challenging but after engaging you can drive fluently.

For small tasks or building from scratch, this code could might help you to begin:

string StoredProcedure = "myStoredProcedureName";
List<SqlParameter> Parameters = new List<SqlParameter>();
SqlParameter param1 = new SqlParameter("TemplateType", CurrentUser.UserID) { SqlDbType = System.Data.SqlDbType.NVarChar, IsNullable = false }
Parameters.Add(param1);
string connectionString = ""; // Put your connection string here
using (SqlConnection Connection = new SqlConnection(connectionString))
{
    Connection.Open();

    SqlCommand Command = new SqlCommand(StoredProcedure, Connection);
    Command.CommandType = CommandType.StoredProcedure;
    Command.CommandTimeout = 60 * 5; // 5 minutes
    Command.Parameters.AddRange(Parameters.ToArray());

    List<string> MyStringsList = new List<string>();

    // the bellow lines are taken from here:
    // https://stackoverflow.com/questions/4018114/read-data-from-sqldatareader
    using(SqlDataReader rdr = Command.ExecuteReader())
    {       
        while (rdr.Read())
        {
            var myString = rdr.GetString(0); //The 0 stands for "the 0'th column", so the first column of the result.
            // Do somthing with this rows string, for example to put them in to a list
            MyStringsList.Add(myString);
        }
    }
    
}
ShayD
  • 689
  • 7
  • 17