Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to increment the database value with every click of the button.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace online_test
{
    public partial class Form1 : Form
    {
        int incre = 0;
        public Form1()
        {
            InitializeComponent();
            radioButton1.TabStop = false;
            radioButton2.TabStop = false;
            radioButton3.TabStop = false;
            radioButton4.TabStop = false;
            //qn1();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //string source_1 = @"DataSource=(LocalDB)\MSSQLLocalDB;" + @"AttachDbFilename=C: \Users\NASH\Documents\Visual Studio 2015\Projects\C#\online test\online test\Database1.mdf;" + "Integrated Security=True;" + "database=<test.mdf';";
            string source_1 = @"Data Source = (LocalDB)\MSSQLLocalDB;" + @"AttachDbFilename=C:\Users\NASH\Documents\Visual Studio 2015\Projects\C#\online test\online test\test.mdf;" + "Integrated Security = True";


            SqlConnection Con_1 = new SqlConnection(source_1);
            {
                Con_1.Open();


                MessageBox.Show("connected to db");

                String sqlSelectQuery = @"SELECT * FROM[Table]";
                SqlCommand cmd = new SqlCommand(sqlSelectQuery, Con_1);
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    //incre++;


                    {
                        label2.Text = (dr["Question"].ToString());
                        textBox1.Text = (dr["sl"].ToString());
                        radioButton1.Text = (dr["Option A"].ToString());
                        radioButton2.Text = (dr["Option B"].ToString());
                        radioButton3.Text = (dr["Option C"].ToString());
                        radioButton4.Text = (dr["Option D"].ToString());
                    }


                    incre = Convert.ToInt32(textBox1.Text);
                   // incre++;
                    // @"SELECT * FROM[Table]WHERE sl=",incre;

                }

                Con_1.Close();
            }

        }
        //button1_click is next button
        private void button1_Click(object sender, EventArgs e)
        {
           incre++;
           String sqlSelectQuery = @"SELECT * FROM[Table] WHERE sl=incre";
           if (dr.Read())
                {
                    //incre++;


                    {
                        label2.Text = (dr["Question"].ToString());
                        textBox1.Text = (dr["sl"].ToString());
                        radioButton1.Text = (dr["Option A"].ToString());
                        radioButton2.Text = (dr["Option B"].ToString());
                        radioButton3.Text = (dr["Option C"].ToString());
                        radioButton4.Text = (dr["Option D"].ToString());
                    }


        }
        // button3_click is previous button
        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
Posted
Updated 24-May-18 0:51am

Try:
SQL
UPDATE MyTable SET MyColumn = MyColumn + 1 WHERE ...
 
Share this answer
 
To change the database value(s) you need to perform the UPDATE statement, not SELECT!
 
Share this answer
 
v2

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