Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to write a function to insert a value in the specified database.But if the entered value(the value is in a textbox) is repeated then it should not b stored and a message should be displayed.kindly help me out with the structure of this function...
thanks in advance..:)
Posted
Updated 10-Mar-13 20:29pm
v2
Comments
gvprabu 11-Mar-13 2:18am    
you are asking ASP.Net or SQL Server?
Ashit Mehra 11-Mar-13 2:28am    
I am asking from asp.net to sql server
What have you tried so far ?
Post some codes.

1 solution

C#
private DataSet GetDataByName(string Name)
{
SqlConnection conn  =new SqlConnection ( "Data Source=VU-PC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
            conn.Open();
SqlCommand comm =new SqlCommand( "Select * from Table where Name = " + Name,conn);
SqlDataAdapter myAdapter = new SqlDataAdapter();
       myAdapter.SelectCommand = comm ;
DataSet myDataSet = new DataSet();
myAdapter.Fiill(myDataSet);
conn.Close();
return myDataSet
}

C#
private void InsertInDB()
{
if(GetDataByName(this.txtName.Text).Tables[0].Rows.Count == 0)
{
SqlConnection conn  =new SqlConnection ( "Data Source=VU-PC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
            conn.Open();
            SqlCommand comm =new SqlCommand( "INSERT INTO connect_com "+"(name,E_mail,School) "+" VALUES("+txtName.Text+"','"+"','"+txtEmail.Text+"','"+txtSchool.Text+")",conn);
            
 }          
            comm.ExecuteNonQuery();

            conn.Close();
} 


should it help you?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900