.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Not working CD(create and Delete) - Any one solve

Posted By: Nachi     Posted Date: January 11, 2012    Points:5   Category :ASP.Net
Hi I am beginner of the ASP.NET

Error is not showing but data is not inserting to the table.

C# coding

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Configuration;

public partial class n_Default : System.Web.UI.Page
{
SqlConnection ccon;
int t;
protected void Page_Load(object sender, EventArgs e)
{
ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["ncon"].ToString());
ccon.Open();
}

public void clear()
{
t1.Text = " ";
t2.Text = " ";
}

protected void btn_click1(object sender, EventArgs e)
{
string str = "insert into training..tnachi values('" + t1.Text + "','" + t2.Text + "')";
SqlCommand cmd = new SqlCommand(str,ccon);
cmd.ExecuteNonQuery();
if (t > 0)
{

l3.Text = "User Insert Success";
l3.Visible = true;
clear();
}
else
{
l3.Text = "Error on Insert";
l3.Visible = true;
clear();
}
}

protected void btn_click2(object sender, EventArgs e)
{

string str = "delete from training..tnachi where name='" + t1.Text + "' and pass='" + t2.Text + "'";
SqlCommand cmd = new SqlCommand(str,ccon);
cmd.ExecuteNonQuery();
if (t > 0)
{
clear();
l3.Text = "User Delete Success";
l3.Visible = true;

}
else
{
clear();
l3.Text = "Error on Delete";
l3.Visible = true;
}
}



protected void btn_click3(object sender, EventArgs e)
{

}
}


Asp coding
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeFile="welcome.aspx.cs" Inherits="n_Default" %>
<form runat="server">
<table>
<tr>
<td>
<asp:Label ID="l1" runat="server" Text="Username:"></asp:Label>
<asp:TextBox ID="t1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="l2" runat="server" Text="Password:"></asp:Label>
<asp:TextBox ID="t2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:button ID="b1" runat="server" Text="Insert" OnClick="btn_click1"></asp:button>
<asp:button ID="b2" runat="server" Text="Delete" OnClick="btn_click2"
style="position:relative; top: 0px; left: 80px; width: 52px;"></asp:button>
<asp:button ID="b3" runat="server" Text="updated" OnClick="btn_click3"
style="position:relative; top: 0px; left: 120px; width: 65px;"></asp:button>
</td>
</tr>
<tr>
<td>
<asp:Label ID="l3" runat="server" Text="Error" style="color:Red" visible="false"></asp:Label>
</td>
</tr>
</table>
</form>


Sql coding

create table tnachi
(
ID int identity(1,1),
name varchar(30),
pass varchar(30)
)

insert into table training..tnachi values('new','new')


Note: Training is database and tnachi is a table

Responses
Author: Deco             
Posted Date: January 11, 2012     Points: 20   

Please change the method like this

protected void btn_click1(object sender, EventArgs e)
{
string str = "insert into training..tnachi values('" + t1.Text + "','" + t2.Text + "')";
SqlCommand cmd = new SqlCommand(str,ccon);
try
{
t = cmd.ExecuteNonQuery();
}
catch(Exception Ex)
{
Response.Write(ex.Message);
}
if (t > 0)
{

l3.Text = "User Insert Success";
l3.Visible = true;
clear();
}
else
{
l3.Text = "Error on Insert";
l3.Visible = true;
clear();
}
}



Regards,
Deco
Author: Nachi             
Posted Date: January 11, 2012     Points: 20   

Thanks alwyn,

I will try and say you. Before i will not cover the topic of try and catch. can you explain why my coding not working and little bit about try and catch.



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend