Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
2
Reply
bar code data and Update/insert
Fanan Hassan
Jan 8 2018 5:54 PM
174
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
Upload Source Code
Select only zip and rar file.
Post
Reset
Answers (
2
)
Can I add text into a listView of icon ?
storing text files to data base