473,499 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.Net page with code behind in C#

1 New Member
I have a ASP.Net Page with code behined in C#. as below
Expand|Select|Wrap|Line Numbers
  1. <body>
  2.     <form id="form1" runat="server">
  3.     <table align=center >
  4.     <tr>
  5.         <td>
  6.             <asp:Label runat=server ID="lblCustomer">Customer Name: </asp:Label>
  7.             <asp:TextBox Style="color:Red" runat=server ID="txtCustomer" OnFocus="clearDefault(this)">Enter Customer Name</asp:TextBox>&nbsp&nbsp&nbsp
  8.             <asp:Button runat=server ID=cmdSubmit OnClick="cmdSubmit" Text="Create Customer"/>
  9.         </td>
  10.     </tr>
  11. </table>
  12.     </form>
  13. </body>
  14. </html>
  15.  
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.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using Conversion_Tool.Classes;
  13. namespace Conversioin_Tool
  14. {
  15.     public partial class CustomerCreation : System.Web.UI.Page
  16.     {
  17.         protected void Page_Load(object sender, EventArgs e)
  18.         {
  19.             cmdCustomer_Click += new System.EventHandler(this.cmdCustomer_Click);
  20.  
  21.         }
  22.         protected void cmdCustomer_Click(object sender, EventArgs e)
  23.         {
  24.             CustomerInsert(txtCustomer.Text).ToString;
  25.         }
  26.         protected void CustomerInsert(string Customer)
  27.         {
  28.             string SQLStr = "Insert into CT_Customers" +
  29.             " (Customer)" +
  30.             " Values('" + sCustomer + "')";
  31.  
  32.             SqlConnection SqlConn = new SqlConnection  (ConfigurationManager.ConnectionStrings["Conversion_Tool"].ToString());
  33.  
  34.             try
  35.             {
  36.                 //Try to open connection
  37.                 SQLConn.Open();
  38.  
  39.                 //SQL Adapter
  40.                 SqlDataAdapter da = new SqlDataAdapter(SQLStr, SqlConn);
  41.  
  42.                 //Cose Connection
  43.                 SqlConn.Close();
  44.             }
  45.  
  46.         }
  47.     }
  48. }
  49.  
Mar 9 '10 #1
4 3086
jhardman
3,406 Recognized Expert Specialist
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
Mar 11 '10 #2
semomaniz
210 Recognized Expert New Member
I hope this helps
Expand|Select|Wrap|Line Numbers
  1. string strSql = "INSERT INTO CT_Customers(Customer) Values (@customer);
  2.  
  3. SqlConnection conn = new SqlConnection();
  4. String constr = ConfigurationManager.ConnectionStrings[("COnversion_Tool")].ConnectionString;
  5. conn = new SqlConnection(constr);
  6. SqlCommand mycommand = new SqlCommand(strSql, conn);
  7.  
  8. SqlParameter cust = new SqlParameter("@customer", SqlDbType.VarChar);
  9. mycommand.Parameters.Add(cust);
  10. mycommand.Parameters["@customer"].Value = txtCustomer.Text; //value from the text box
  11.  
  12. try
  13. {
  14.     conn.Open();
  15.     mycommand.ExecuteNonQuery();
  16. }
  17. catch (Exception ex)
  18. {
  19.     throw ex; // Add you error msg display here 
  20. }
  21. finally
  22. {
  23.     conn.Close();
  24. }
  25.  
Mar 11 '10 #3
Frinavale
9,735 Recognized Expert Moderator Expert
For a quick overview of how to use a database in your application check out:
-Frinny
Mar 16 '10 #4
AnagJohari
96 New Member
@Kratos2005
i send u some code just modify according to the table & u use,
Expand|Select|Wrap|Line Numbers
  1. public static void AddDummy(DummyBLL dummybll)
  2.    {
  3.         /* we can use any one of the statement for making an connection, currently we are using a connection string from web.config file.
  4.        SqlConnection con = new SqlConnection("Data Source=localhost\\SQLEXPRESS;Initial Catalog=mahesh;Integrated Security=True");*/
  5.        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
  6.  
  7.         SqlCommand cmd = con.CreateCommand();
  8.        cmd.CommandText = string.Format("Insert into ManageClient(ClientName,Address,MobileNo,Image)Values(@ClientName,@Address,@MobileNo,@Image)");
  9.         con.Open();
  10.         cmd.Parameters.Add(new SqlParameter("@ClientName",dummybll.ClientName));
  11.         cmd.Parameters.Add(new SqlParameter("@Address",dummybll.Address));
  12.         cmd.Parameters.Add(new SqlParameter("@MobileNo",dummybll.MobileNo));
  13.         cmd.Parameters.Add(new SqlParameter("@Image",dummybll.Image));
  14.         cmd.ExecuteNonQuery();
  15.         con.Close();
  16.     }
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(object) & call
May 5 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6890
by: Fred Armitage | last post by:
I have a fairly complex application written using a vb code-behind dll. However, I'd like to write one particular aspx page utilising in-line code so that it can easily be modified as needed by the...
3
2074
by: Jeff Cooper | last post by:
Hey Folks, I have a bunch of aspx pages in a solution. One of these pages, call it header1.aspx, has it's code behind in header1.aspx.vb. Another, header2.aspx, has header2.aspx.vb. When I...
6
11264
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
5
2034
by: Invalidlastname | last post by:
Hi, I just read the pattern "Design and Implementation Guidelines for Web Clients" from MSDN. Here is my question. In chapter 3,...
7
2745
by: Cliff Cavin | last post by:
How do I programatically change the background color of an aspx page in code? Is there something like Page.BgColor = "#FFFFFF" ? For that matter, how do I access any of the DOCUMENT...
1
3386
by: William Parker | last post by:
I have a web control I made called header.ascx. It has its own properties and methods I defined. But I cannot figure out how to access this control from my code behind page. I can create the...
2
1958
by: N. Demos | last post by:
I have a user control with code behind of which two instances are created/declared in my aspx page. The aspx page has code behind also, as I need to access methods of the usercontrols on page...
7
1732
by: Alan Silver | last post by:
Hello, I have installed the 2.0 framework, and am looking at converting some of my 1.1 pages to use partial classes. I don't (yet) have VS2005, so I'm doing this by hand, but am having problems....
3
3923
by: Pierre | last post by:
Hello, In an aspx page (mypage.aspx) from a web projet, I would like to get the value of a variable of the projet that is declared as public in a module. The variable can be called from...
4
1591
by: Yourself | last post by:
Is there anyway of processing an ASP.NET page after the code behind has run, but before it's presented as HTML? What I'm trying to do is alter some of the markup that a content management system...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.