web part not getting connected with the database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srig
    New Member
    • Feb 2009
    • 11

    web part not getting connected with the database

    hi all,
    i hav created a webpart for inserting a record in asp.net ,c# and deploy it in sharepoint.. my code is executing properly.. when i deploy it it asks for insert new record and wen i get the details and click insert the values are not getting stored in sql server database.. could any one point me out what s wrong in the program..i hav posted my prog below

    Thanks
    Sri

    Code:
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Security;
    using System.Web.Security;
    using Microsoft.SharePoint.WebPartPages;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Xml;
    using System.Security.Permissions;
    
    namespace dweb
    {
    	/// <summary>
    	/// Summary description for WebCustomControl1.
    	/// </summary>
    	[DefaultProperty("Text"),
    	ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    	public class dweb:WebPart
    	{
    		
    		//private string text;
    
    		[Bindable(true),
    		Category("Appearance"),
    		DefaultValue("")]
    			
    		Label lid=new Label();
    		TextBox txtid=new TextBox();
    		Label lfname=new Label();
    		TextBox txtfname=new TextBox();
    		Label lname=new Label();
    		TextBox txtlname=new TextBox();
    		Label addr=new Label();
    		TextBox txtaddr=new TextBox();
    		Label email=new Label();
    		TextBox txtemail=new TextBox();
    		TextBox txtphno=new TextBox();
    		TextBox txtdesig=new TextBox();
    		TextBox txtsal=new TextBox();
    		TextBox txtqual=new TextBox();
    		TextBox txtage=new TextBox();
    		
    
    		SqlConnection con;
    		
    		protected override void CreateChildControls()
    		{
    			Button insert=new Button();
    			insert.Text="Insert New Record";
    			insert.ID="create";
    			Controls.Add(insert);
    			Controls.Add(new LiteralControl("<br>"));
    			insert.Click+=new EventHandler(create_Click);
    		
    			EnsureChildControls();
    		}
    		
    		private void create_Click(object sender, EventArgs e)
    		{
    			
    			
    			lid.Text="Enter ID:";
    			Controls.Add(lid);
    			Controls.Add(txtid);
    			Controls.Add(new LiteralControl("<br>"));
    			lfname.Text="First Name:";
    			Controls.Add(lfname);
    			Controls.Add(txtfname);
    			Controls.Add(new LiteralControl("<br>"));
    			lname.Text="Last Name:";
    			Controls.Add(lname);
    			
    			Controls.Add(txtlname);
    			Controls.Add(new LiteralControl("<br>"));
    		
    			addr.Text="Address:";
    			Controls.Add(addr);
    			
    			Controls.Add(txtaddr);
    			Controls.Add(new LiteralControl("<br>"));
    			
    			email.Text="Email Id:";
    			Controls.Add(email);
    			
    			Controls.Add(txtemail);
    			Controls.Add(new LiteralControl("<br>"));
    			Label phno=new Label();
    			phno.Text="Phone No:";
    			Controls.Add(phno);
    			
    			Controls.Add(txtphno);
    			Controls.Add(new LiteralControl("<br>"));
    			Label desig=new Label();
    			desig.Text="Designation:";
    			Controls.Add(desig);
    		
    			Controls.Add(txtdesig);
    			Controls.Add(new LiteralControl("<br>"));
    			Label sal=new Label();
    			sal.Text="Salary:";
    			Controls.Add(sal);
    			
    			Controls.Add(txtsal);
    			Controls.Add(new LiteralControl("<br>"));
    			Label qual=new Label();
    			qual.Text="Qualification:";
    			Controls.Add(qual);
    			
    			Controls.Add(txtqual);
    			Controls.Add(new LiteralControl("<br>"));
    			Label age=new Label();
    			age.Text="Age:";
    			Controls.Add(age);
    		
    			Controls.Add(txtage);
    			Controls.Add(new LiteralControl("<br>"));
    			Button btncreate=new Button();
    			btncreate.Text="INSERT";
    			btncreate.ID="insertbtn";
    			Controls.Add(btncreate);
    			btncreate.Click +=
    				new EventHandler(insertbtn_Click);
    		}
    
    		[AspNetHostingPermission(SecurityAction.Demand,			 Level=AspNetHostingPermissionLevel.Unrestricted)]
    		 [AspNetHostingPermissionSecurityAction.InheritanceDemand,			 Level=AspNetHostingPermissionLevel.Unrestricted)]
    		
    	private void insertbtn_Click(objectsender,EventArgs e)
    			{
    			
    con = new SqlConnection("Data Source=WSINTRA13;Initial Catalog=pubs; User ID=Insite.user;Password=insiteslc1");
    string cmd = "insert into emp_details(e_id,fname,lname,address,email_id,ph_no,designation,salary,qualifictn,age) values( '" + txtid.Text + " ','" + txtfname.Text +  " ','" + txtlname.Text + "','" + txtaddr.Text+ "','" +txtemail.Text+ "'," +txtphno.Text+ ",'" +txtdesig.Text+ "'," +txtsal.Text+ ",'" +txtqual.Text+ "'," + txtage.Text + ")";		
    
    
                   SqlCommand scmd = new SqlCommand(cmd,con); 
    	scmd.CommandText=cmd;
    	con.Open();
    	scmd.ExecuteNonQuery();
    	con.Close();
    
                   }
    	
    	}
    }
    Last edited by tlhintoq; Mar 11 '09, 03:06 PM. Reason: [CODE] ... [/CODE] tags added
Working...