Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is my sample code

i am getting error at below codes

1)con1.Open();
cmd.ExecuteNonQuery();
2)da.Fill(ds);


i am using access database.

textbox4 & textbox5 are doubles datatype in my access datbase i.e i have to enter only hrs means numerical data

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
using (OleDbConnection con1 = new OleDbConnection(con))
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con1;
OleDbDataAdapter da;

DataSet ds = new DataSet();

if ((textBox4.Text == string.Empty) || (textBox5.Text == string.Empty) || (comboBox1.Text == string.Empty) || (comboBox2.Text == string.Empty))
{

MessageBox.Show("Please enter Your All the Details in the Related Fields ...!!!!!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);


}
else
{
cmd = new OleDbCommand("SELECT * from Spldetails where Date = '" + dateTimePicker1.Text + "' and EmpId = '" + textBox1.Text + "' and Splwrk = '" + comboBox2.Text + "' and Splwrkdetails = '" + textBox6.Text + "'", con1);
con1.Open();
da = new OleDbDataAdapter(cmd);
da.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if (i > 0)
{
MessageBox.Show("Duplicate Details !!!! You have already Submitted your details on " + dateTimePicker1.Text + ".");
//ds.Clear();
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";

}
else
{

try
{


string command = "INSERT INTO Spldetails([Date],[EmpId],[EmpName],[Designation],[Records],[Splwrkhrs],[Team],[Splwrk],[Splwrkdetails],[Allottedby]) VALUES('" + this.dateTimePicker1.Text + "','" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.textBox4.Text + "','" + this.textBox5.Text + "','" + this.textBox8.Text + "','" + this.comboBox2.Text + "','" + this.textBox6.Text + "','" + this.comboBox1.Text + "')";
cmd = new OleDbCommand(command, con1);
con1.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Congratulations....Your Details have been submitted successfully");

textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";
dataGridView1.Refresh();

}

catch (Exception exp)
{
MessageBox.Show("error" + exp);
}

}
}

}
}
Posted
Updated 31-May-18 8:33am

C#
string command = "INSERT INTO Spldetails([Date],[EmpId],[EmpName],[Designation],[Records],[Splwrkhrs],[Team],[Splwrk],[Splwrkdetails],[Allottedby]) VALUES('" + this.dateTimePicker1.Text + "','" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.textBox4.Text + "','" + this.textBox5.Text + "','" + this.textBox8.Text + "','" + this.comboBox2.Text + "','" + this.textBox6.Text + "','" + this.comboBox1.Text + "')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
Comments
Prateek gsharma 1-Jun-18 1:31am    
u mean to say i have to use parameterized queries.ok i will use now.

tell me the solution sir?
Patrice T 1-Jun-18 1:42am    
Because of the way you construct the SQL command, a syntax error depend on the contain of textboxes.
You need to use parameters in your select statement, not concat strings together. This will allow you you specify the types of the values in your where clause. for example:
string sqlStr = "SELECT [MenuID],[MenuText],[URL]"
                            + "  FROM [dbo].[ServiceDeskMenu]"
                            + "  WHERE [ParentID] = @parent_type"
                            + "  ORDER by[Position] ";
            SqlParameter param = new SqlParameter
            {
                ParameterName = "@parent_type",
                DbType = DbType.Int32,
                Value = parentType
            };

then you can add the param object to you sql command object and execute your query

sqlCommand cmd = new SqlCommand(sqlStr, conn);
                if(param!=null)
                { 
                    cmd.Parameters.Add(param);
                }
 
Share this answer
 
Comments
Prateek gsharma 1-Jun-18 2:02am    
ok i will try sir.
One more thing, don't select *. Just select the fields you want.
 
Share this answer
 
Comments
Jinto Jacob 1-Jun-18 0:36am    
Hi please don't create a new solution to add lines to your previous answer. you can edit the answer using improve solution button and add lines to the 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