Click here to Skip to main content
15,918,268 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
string OrderID = "";
string str="insert into tblOrderUser values('" + textBox1.Text + "','" + textBox2.Text + "','" + comboBilltype.Text + "','" + dateTimePicker1.Value.ToString("dd/MM/yyyy") + "'),con";
SqlCommand cmd1 = con.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = str;
cmd1.ExecuteNonQuery();

Above code am inserting value to table tblOrder , every thing is correct but except data pivker , all the value showing null...am very confusion can anu body help me for this.

What I have tried:

C#
string OrderID = "";
string str="insert into tblOrderUser values('" + textBox1.Text + "','" + textBox2.Text + "','" + comboBilltype.Text + "','" + dateTimePicker1.Value.ToString("dd/MM/yyyy") + "'),con";
SqlCommand cmd1 = con.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = str;
cmd1.ExecuteNonQuery();

SqlCommand cmd2 = con.CreateCommand();
cmd2.CommandType = CommandType.Text;
//cmd2.CommandText = "insert into tblOrderItem values('" + txtproduct.Text + "','" + txtPrice.Text + ",'" + txtQty.Text + "',;" + txttotal.Text + "')";
cmd2.CommandText = "select top 1 * from tblOrderUser order by OUid desc";
cmd2.ExecuteNonQuery();
DataTable dt2 = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd2);
adp.Fill(dt2);
Posted
Updated 6-Sep-20 19:20pm
v2

Advice: print the queries after concatenation to see what they are exactly.
C#
string str="insert into tblOrderUser values('" + textBox1.Text + "','" + textBox2.Text + "','" + comboBilltype.Text + "','" + dateTimePicker1.Value.ToString("dd/MM/yyyy") + "'),con";

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[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
v2
Comments
Brijesh C G 7-Sep-20 13:27pm    
Thanks for your support
Quote:
every thing is correct but except data pivker

This means that you need to figure out and handle the value being assigned to datapicker. Above code is not responsible for its NULL value. Though, since it's null, trying to use ToString on it will raise an error "Object reference not set to an instance".

This is one of the common errors asked here - Null reference exception.

Details about the error you see: NullReferenceException Class (System) | Microsoft Docs[^]
Quote:
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null

Possibly because:
1. You've forgotten to instantiate a reference type
2. You've forgotten to dimension an array before initializing it.
3. You get a null return value from a method, and then call a method on the returned type.
4. You're using an expression (for example, you're chaining a list of methods or properties together) to retrieve a value
5. You're enumerating the elements of an array that contains reference types, and your attempt to process one of the elements throws
6. Exception is thrown by a method that is passed null

When you debug, you will be able to get the exact line where the variable is NULL and error is being raised. Find why it's null and handle it.

Other response here has already shard about the SQL injection issue that you need to handle as best practice.

Try out.
 
Share this answer
 
v3
Comments
Brijesh C G 7-Sep-20 13:28pm    
Thanks for your support

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

  Print Answers RSS


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