Hi Team,
how do i counter check the data which am entering through the textbox with the data in the current database.
for instance if i have store code "001" in the database, then i try entering the same code"001" using c# in asp.net it should give me an error like this "the code already exist"
regard
George
Loading
Satyapriya NayakPosted Apr 5, 2012, 1:53 PM
Try this...
Table
Create table product(code varchar(50))
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Counter_check_data._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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 System.Data.SqlClient;
namespace Counter_check_data
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str = null;
protected void btn_code_Click(object sender, EventArgs e)
{
Codecheck();
}
public void Codecheck()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select count(*)from product where code='" + txtCode.Text + "'";
com = new SqlCommand(str, con);
int count = Convert.ToInt32(com.ExecuteScalar());
if (count > 0)
{
lblMessage.Text = "Sorry! you can't take this code";
}
else
{
lblMessage.Text = "You can take this code";
}
}
}
}
Thanks
If this post helps you mark it as answer
SenthilkumarPosted Apr 5, 2012, 11:24 AM
If you want to do this explicitly then you can use the select statement with passed value
SELECT * FROM Products WHERE code=001
If you use the stored procedure then
I suggest you to use the auto increment value, if you want to do serial number
Code INT IDENTITY(1,1)
Hope this will help you.