ASP.Net page with code behind in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kratos2005
    New Member
    • Mar 2010
    • 1

    ASP.Net page with code behind in C#

    I have a ASP.Net Page with code behined in C#. as below
    Code:
    <body>
        <form id="form1" runat="server">
        <table align=center >
        <tr>
            <td>
                <asp:Label runat=server ID="lblCustomer">Customer Name: </asp:Label>
                <asp:TextBox Style="color:Red" runat=server ID="txtCustomer" OnFocus="clearDefault(this)">Enter Customer Name</asp:TextBox>&nbsp&nbsp&nbsp
                <asp:Button runat=server ID=cmdSubmit OnClick="cmdSubmit" Text="Create Customer"/>
            </td>
        </tr>
    </table>
        </form>
    </body>
    </html>
    I am try to send the Customer name entered to a SQL DB but I am not sure how to process this on the C# page. I am new to this and do not quite understand. I have been able to retrive data in other apps but I can not seem to write to a SQL DB. Below is what I have in the code behind.
    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using Conversion_Tool.Classes;
    namespace Conversioin_Tool
    {
        public partial class CustomerCreation : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                cmdCustomer_Click += new System.EventHandler(this.cmdCustomer_Click);
    
            }
            protected void cmdCustomer_Click(object sender, EventArgs e)
            {
                CustomerInsert(txtCustomer.Text).ToString;
            }
            protected void CustomerInsert(string Customer)
            {
                string SQLStr = "Insert into CT_Customers" +
                " (Customer)" +
                " Values('" + sCustomer + "')";
    
                SqlConnection SqlConn = new SqlConnection  (ConfigurationManager.ConnectionStrings["Conversion_Tool"].ToString());
    
                try
                {
                    //Try to open connection
                    SQLConn.Open();
    
                    //SQL Adapter
                    SqlDataAdapter da = new SqlDataAdapter(SQLStr, SqlConn);
    
                    //Cose Connection
                    SqlConn.Close();
                }
                    
            }
        }
    }
    Last edited by RedSon; Mar 9 '10, 10:42 PM. Reason: [CODE] tags please!!! For the love of all that is holy!
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    You posted in the ASP classic forum, instead of the ASP.NET forum. I'll move it over for you, but next time please note that there is a really big difference between ASP and ASP.NET. No solutions I can give you in ASP will work in an ASP.NET website.

    Jared

    Comment

    • semomaniz
      Recognized Expert New Member
      • Oct 2007
      • 210

      #3
      I hope this helps
      Code:
      string strSql = "INSERT INTO CT_Customers(Customer) Values (@customer);
      
      SqlConnection conn = new SqlConnection();
      String constr = ConfigurationManager.ConnectionStrings[("COnversion_Tool")].ConnectionString;
      conn = new SqlConnection(constr);
      SqlCommand mycommand = new SqlCommand(strSql, conn);
      
      SqlParameter cust = new SqlParameter("@customer", SqlDbType.VarChar);
      mycommand.Parameters.Add(cust);
      mycommand.Parameters["@customer"].Value = txtCustomer.Text; //value from the text box
      
      try
      {
      	conn.Open();
      	mycommand.ExecuteNonQuery();
      }
      catch (Exception ex)
      {
      	throw ex; // Add you error msg display here 
      }
      finally
      {
      	conn.Close();
      }

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        For a quick overview of how to use a database in your application check out:

        -Frinny

        Comment

        • AnagJohari
          New Member
          • May 2010
          • 96

          #5
          Originally posted by Kratos2005
          I have a ASP.Net Page with code behined in C#. as below
          Code:
          <body>
              <form id="form1" runat="server">
              <table align=center >
              <tr>
                  <td>
                      <asp:Label runat=server ID="lblCustomer">Customer Name: </asp:Label>
                      <asp:TextBox Style="color:Red" runat=server ID="txtCustomer" OnFocus="clearDefault(this)">Enter Customer Name</asp:TextBox>&nbsp&nbsp&nbsp
                      <asp:Button runat=server ID=cmdSubmit OnClick="cmdSubmit" Text="Create Customer"/>
                  </td>
              </tr>
          </table>
              </form>
          </body>
          </html>
          I am try to send the Customer name entered to a SQL DB but I am not sure how to process this on the C# page. I am new to this and do not quite understand. I have been able to retrive data in other apps but I can not seem to write to a SQL DB. Below is what I have in the code behind.
          Code:
          using System;
          using System.Data;
          using System.Configuration;
          using System.Linq;
          using System.Web;
          using System.Web.Security;
          using System.Web.UI;
          using System.Web.UI.HtmlControls;
          using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Xml.Linq;
          using Conversion_Tool.Classes;
          namespace Conversioin_Tool
          {
              public partial class CustomerCreation : System.Web.UI.Page
              {
                  protected void Page_Load(object sender, EventArgs e)
                  {
                      cmdCustomer_Click += new System.EventHandler(this.cmdCustomer_Click);
          
                  }
                  protected void cmdCustomer_Click(object sender, EventArgs e)
                  {
                      CustomerInsert(txtCustomer.Text).ToString;
                  }
                  protected void CustomerInsert(string Customer)
                  {
                      string SQLStr = "Insert into CT_Customers" +
                      " (Customer)" +
                      " Values('" + sCustomer + "')";
          
                      SqlConnection SqlConn = new SqlConnection  (ConfigurationManager.ConnectionStrings["Conversion_Tool"].ToString());
          
                      try
                      {
                          //Try to open connection
                          SQLConn.Open();
          
                          //SQL Adapter
                          SqlDataAdapter da = new SqlDataAdapter(SQLStr, SqlConn);
          
                          //Cose Connection
                          SqlConn.Close();
                      }
                          
                  }
              }
          }
          i send u some code just modify according to the table & u use,
          Code:
          public static void AddDummy(DummyBLL dummybll)
             {
                  /* we can use any one of the statement for making an connection, currently we are using a connection string from web.config file.
                 SqlConnection con = new SqlConnection("Data Source=localhost\\SQLEXPRESS;Initial Catalog=mahesh;Integrated Security=True");*/
                 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
                 
                  SqlCommand cmd = con.CreateCommand();
                 cmd.CommandText = string.Format("Insert into ManageClient(ClientName,Address,MobileNo,Image)Values(@ClientName,@Address,@MobileNo,@Image)");
                  con.Open();
                  cmd.Parameters.Add(new SqlParameter("@ClientName",dummybll.ClientName));
                  cmd.Parameters.Add(new SqlParameter("@Address",dummybll.Address));
                  cmd.Parameters.Add(new SqlParameter("@MobileNo",dummybll.MobileNo));
                  cmd.Parameters.Add(new SqlParameter("@Image",dummybll.Image));
                  cmd.ExecuteNonQuery();
                  con.Close();
              }
          After that call this method in aspx page its work,
          u can use any one of the statements either direct use statements or make a connection string in web cinfig file
          by the way we did the comment of direct methos i only use the connection string make in web.config file
          u use the statement in aspx page is that
          addclient(objec t) & call

          Comment

          Working...