I'm coming from Java programming and I recently just tried to study C# for web (.aspx
). I'm new and still getting myself familiar with the components and how C# components are bound to the SQL Server database.
I'm having trouble setting values from TextBoxes
which are assigned to String
variables into my SQL statement.
Here's my code.
protected void Btn_additem_Click(object sender, EventArgs e)
{
String category = "";
String itemName = Tb_itemname.Text;
String code = Tb_itemcode.Text;
String brand = Tb_brand.Text;
String serial = Tb_serial.Text;
String capacity = Tb_capacity.Text;
String version = Tb_version.Text;
if (Rbl_hardsoft.SelectedValue.Equals("Hardware"))
{
category = "Hardware";
}
else if(Rbl_hardsoft.SelectedValue.Equals("Software"))
{
category = "Software";
}
String SQL = "INSERT INTO ItemMasterData(item_code,item_category, item_name, item_brand,item_serialnumber, item_capacity, item_version) " +
"VALUES(?,?,?,?,?,?,?)";
}
My goal is to be able to make the string variables category
, itemName
, code
...and so on to be arguments for the ?
In Java we usually make use of PreparedStatement
wherein we use ? for arguments then we set its value thru setters. For instance,
ps.setString(1,"stringvalue or string variable"); // 1 for the first question mark
ps.setInt(2,intvalueOrintVariable); // 2 for the second question mark
How do I do that in C#? I'm not very familiar with DataSource
yet and I would like to be able to assign parameters by code rather than by using the C# properties window.
I'd appreciate any help or practical example.
Thanks.