Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

i have two textboxes. both of have some integer value. i want when the user click on the save button then first time its saves in the database next time if the user pass some value then it does't save in the database.
Posted
Comments
Marvin Ma 2-Sep-13 10:08am    
Could you post your code please?
abbaspirmoradi 2-Sep-13 10:11am    
unclear.explain more.. you have two textboxes.what the means of its?are you want just one time your data save into database?
Manish Arya 2-Sep-13 10:17am    
yes i want just one time data save into database

i am using for insert above code:


private void btnSavePrice_Click(object sender, EventArgs e)
{
string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
string query = "insert into PriceTable values('" + txtPriceGold.Text + "','" + txtPriceSilver.Text + "')";
SqlConnection conn = new SqlConnection(connstr);

if (txtPriceGold.Text != "" & txtPriceSilver.Text != "")
{
conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();

txtPriceGold.Clear();
txtPriceSilver.Clear();

MessageBox.Show("Values Save in DataBase");
conn.Close();
}
else
{
MessageBox.Show("Enter some value in the box");
}
}


1 solution

So check if it is in the table before you do the insert, by using SELECT to eitehr return matching records or return the count of the records and check it for zero:
SQL
SELECT COUNT(*) FROM MyTable WHERE myColumn=MyValu
You could cretae a stored procedure to check and then insert if it doesn't exist, but I'd rather do it expicitly and report a problem to the user sensibly instead.
 
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