Click here to Skip to main content
16,017,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose i enter a name in textbox i want to check that the name is already in database or not if it not i want to add the name or i want to update the name with further details

i want to retrieve one column of data from my table and i want to check that column data is contain the data that i enter in my textbox

product1 field occure in zeroth position in product table.
so in if condition i want to check the product is occure in product1 field or not if it occure it goes to if condition statement or not it goes to else statement

C#
string t = "select Product1 from Product";
SqlDataAdapter fd = new SqlDataAdapter(t, DbConnection.mCon);
DataSet ds = new DataSet();
fd.Fill(ds);
if(ds.Tables[0].ToString()==TextBox1.Text)
{
 DateTime date = DateTime.Parse(DateTime.Now.ToShortDateString());
 string dt = date.ToString("MM/dd/yyyy");
 string month = date.ToString("MMMM");
 string vv = "session1";
string s11 = "update Product set Product1='" + TextBox1.Text + "' Quantity='" + TextBox2.Text + "' Price='" + TextBox3.Text + "' Date='" + dt + "' Month='" + month + "' Session='" + vv + "' where Product1='" + TextBox1.Text + "' ";
SqlCommand cmdh = new SqlCommand(s11, DbConnection.mCon);
cmdh.ExecuteNonQuery();
Response.Write("<script>alert('PRODUCT IS UPDATED')</script>");      
}
else
{
 DateTime date = DateTime.Parse(DateTime.Now.ToShortDateString());
 string dt = date.ToString("MM/dd/yyyy");
 string month = date.ToString("MMMM");
 string vv = "session1";
 string s = "insert into Product(Product1,Quantity,Price,Date,Session,Month) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + dt + "','" + vv + "','" + month + "')";
 cmd = new SqlCommand(s, DbConnection.mCon);
 cmd.ExecuteNonQuery();
 Response.Write("<script>alert('NEW PRODUCT INSERTED')</script>");
 TextBox1.Text = TextBox2.Text = TextBox3.Text = "";
}
Posted
Updated 13-Dec-12 18:16pm
v3
Comments
ravuravu 13-Dec-12 23:49pm    
The problem is in if loop.the condition that check textbox value with column data.pls help me
ravuravu 13-Dec-12 23:51pm    
how to check the textbox value is occure or not in database
StackQ 13-Dec-12 23:59pm    
textbox1.text=?
is it containing name of product1
ravuravu 14-Dec-12 0:06am    
yes it containing name of product1
ravuravu 14-Dec-12 0:08am    
i have no grid in it. me only want to check the product is in database or not

C#
most simple way u can do it like->

After 
fd.fill(ds);
DataGridView.dataSource=ds;  //U have a control DataGridView
if(urDataGridViewName.Rows.Count >0)  //means records exist in database SoUpdat
{
//Now apply Update Query
}
else   //Record not in database So Create query will applu
{
//Now Apply Insert Query
}
 
Share this answer
 
v2
Comments
minal21 14-Dec-12 0:23am    
I think,to apply your above if condition (urDataGridViewName.Rows.Count >0) .
Select query should be changes as string t = "select Product1 from Product where product1=TextBox1.Text";

and then can continue with this
fd.fill(ds);
DataGridView.dataSource=ds; //U have a control DataGridView
if(urDataGridViewName.Rows.Count >0) //means records exist in database SoUpdat
{
//Now apply Update Query
}
else //Record not in database So Create query will applu
{
//Now Apply Insert Query
}
StackQ 14-Dec-12 0:26am    
ya u r right,but i think he know well how to write query.
ravuravu 14-Dec-12 0:47am    
sir ur assumption is correct i changed my query to ur method its the correct way
ravuravu 14-Dec-12 0:52am    
thanks to all of them for helping me.
StackQ 14-Dec-12 0:53am    
ok. 1st i saw ur code,and according to ur code i assume if u can write->
string s = "insert into Product(Product1,Quantity,Price,Date,Session,Month) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + dt + "','" + vv + "','" + month + "')";

this code,then u have ability to think what should change.
ok,try more,and u can learn more from this website.
Hi,

Here you are 1st populating the data from the database and checking whether that value matches with your textbox valus in the if statement. Instead of doing that you canoptimize your query like this:

string t = "select * from Product where Product1 = " + TextBox1.Text;
SqlDataAdapter fd = new SqlDataAdapter(t, DbConnection.mCon);
DataSet ds = new DataSet();
fd.Fill(ds);
if(ds.Tables[0].Rows.Count > 0)
{
 // Your operations that you want to do.      
}
else
{
 // The operations that you want to do here.
}


This will reduce the time taken for your data retrieval.

Thanks
 
Share this answer
 
Comments
ravuravu 14-Dec-12 0:45am    
very thank u sir that s the answer
[no name] 14-Dec-12 1:13am    
If you are satisfied with the answer and your requirement is achieved, please Upvote and accept it. Never downvote to any correct ans as it reduces point.
ravuravu 14-Dec-12 4:21am    
sir i vote it
[no name] 14-Dec-12 10:33am    
If you vote it then vote up to 5 stars so that it will be considered as up voted. otherwise it will be down voted. And accept the solution if that helped you.

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