COMMUNITY: How to properly ask a question on Forums
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
C# Corner Home
Technologies
Monthly Leaders
ASK A QUESTION
Forum guidelines
Fanan Hassan
1.6k
17
130
bar code data and Update/insert
Jan 8 2018 5:54 PM
Guys i am new to C# and stuck on below problem
1. i am not able to update data as per below code. insert is working normally but not the update. some syntax error which i cant figure out.
2. barcode is populated but not able to either insert/ update. gives me error. while if input normal number from my keyboard is gets inserted in database. so why barcode is not getting checked on SELECT command.
private
void
button1_Click(
object
sender, EventArgs e)
{
SqlConnection cn =
new
SqlConnection(global::Inventory_System.Properties.Settings.Default.Database1ConnectionString);
cn.Open();
SqlCommand cmdCount =
new
SqlCommand(
"SELECT count(*) from Stock WHERE ProductCode = @ProductCode"
, cn);
cmdCount.Parameters.AddWithValue(
"ProductCode"
, tbstockpc.Text);
int
count = (
int
) cmdCount.ExecuteScalar();
if
(count > 0)
{
try
{
int
Stprice = 0;
int
qty = Convert.ToInt32(tbstockqty.Text);
int
puprice = Convert.ToInt32(tbstockpurprice.Text);
Stprice = qty * puprice;
string
sql =
"Update Stock(ProductCode,ProductDesc,Quantity,Stockdate,UnitPrice,UnitPricePur,StockPrice) values('"
+ tbstockpc.Text +
"','"
+ tbstockdesc.Text +
"','"
+ tbstockqty.Text +
"','"
+ tbstockdate.Text +
"','"
+ tbstocksaleprice.Text +
"','"
+ tbstockpurprice.Text +
"', '"
+ Stprice +
"' )"
;
SqlCommand exeSql =
new
SqlCommand(sql, cn);
exeSql.ExecuteNonQuery();
this
.StockTableAdapter.Update(
this
.database1DataSet.Stock);
MessageBox.Show(
"Data is Updated to Database"
,
"Stock Entered"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message,
"Error"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cn.Close();
tbstockdesc.Text =
" "
;
tbstockpc.Text =
" "
;
tbstockpurprice.Text =
" "
;
tbstockqty.Text =
" "
;
tbstocksaleprice.Text =
" "
;
}
}
else
{
try
{
int
Stprice = 0;
int
qty = Convert.ToInt32(tbstockqty.Text);
int
puprice = Convert.ToInt32(tbstockpurprice.Text);
Stprice = qty * puprice;
string
sql =
"insert Stock(ProductCode,ProductDesc,Quantity,Stockdate,UnitPrice,UnitPricePur,StockPrice) values('"
+ tbstockpc.Text +
"','"
+ tbstockdesc.Text +
"','"
+ tbstockqty.Text +
"','"
+ tbstockdate.Text +
"','"
+ tbstocksaleprice.Text +
"','"
+ tbstockpurprice.Text +
"', '"
+ Stprice +
"' )"
;
SqlCommand exeSql =
new
SqlCommand(sql, cn);
exeSql.ExecuteNonQuery();
MessageBox.Show(
"Data is Inserted to Database"
,
"Stock Entered"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
//this.stockTableAdapter.Fill(this.database1DataSet.stock);
this
.StockTableAdapter.Fill(
this
.database1DataSet.Stock);
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message,
"Error"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cn.Close();
tbstockdesc.Text =
" "
;
tbstockpc.Text =
" "
;
tbstockpurprice.Text =
" "
;
tbstockqty.Text =
" "
;
tbstocksaleprice.Text =
" "
;
}
}
}
Reply
Answers (
2
)
Can I add text into a listView of icon ?
storing text files to data base
"