Reputation: 935
I'm currently building a database font end and I'm currently stuck. I'm trying to take the List item of a datasourced drop down list and use that as a parameter in a SQL query. I then wan each column from those results to be displayed in a text box. Here's some code!
On button event:
protected void ButtonAsset_Click(object sender, EventArgs e)
{
ExecuteSelect(DropDownListAsset.Selecteditem.Text);
}
Getting the Connection String:
public string GetConnectionStringMyConn()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["my_conn"].ConnectionString;
}
The Select method:
private void ExecuteSelect(string Aname)
{
SqlConnection connection = new SqlConnection(GetConnectionStringMyConn());
using (var command = connection.CreateCommand())
{
command.CommandText = "Select tblAssets.AssetID, tblAssets.Domain, tsysOS.OSname, tblAssets.SP,"
+ "tblAssets.Memory, tblAssets.Processor, tblAssetCustom.Manufacturer, tblAssetCustom.Model)"
+ "FROM tblAssets"
+ "INNER JOIN tblAssetsCustom ON tblAssets.AssetID = tblAssetCustom.AssetID "
+ "INNER JOIN tsysOS ON tblAssets.OScode = tsysOS.OScode "
+ "WHERE tblAssets.AssetName = @AssetName";
connection.Open();
SqlParameter[] Aparam = new SqlParameter[1];
Aparam[0] = new SqlParameter("@AssetName", SqlDbType.NVarChar);
Aparam[0].Value = Aname;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
TextBoxAssetID.Text = reader["AssetID"].ToString();
TextBoxDomain.Text = reader["Domain"].ToString();
TextBoxOS.Text = reader["OSname"].ToString();
TextBoxSP.Text = reader["SP"].ToString();
TextBoxMemory.Text = reader["Memory"].ToString();
TextBoxProcessor.Text = reader["Processor"].ToString();
TextBoxManufacturer.Text = reader["Manufacturer"].ToString();
TextBoxModel.Text = reader["Model"].ToString();
}
connection.Close();
}
}
Any help would be much appreciated! I keep getting syntax errors on using (var reader = command.ExecuteReader()) and I cannot understand why. I've been using this Get SQL data And show it in a text box? for the reader
Upvotes: 1
Views: 187
Reputation: 4963
Do Like this
SqlCommand command= new SqlCommand();
command.Connection = connection;
command.CommandText = "Select tblAssets.AssetID, tblAssets.Domain, tsysOS.OSname, tblAssets.SP,"
+ " tblAssets.Memory, tblAssets.Processor, tblAssetCustom.Manufacturer, tblAssetCustom.Model"
+ " FROM tblAssets"
+ " INNER JOIN tblAssetsCustom ON tblAssets.AssetID = tblAssetCustom.AssetID "
+ " INNER JOIN tsysOS ON tblAssets.OScode = tsysOS.OScode "
+ " WHERE tblAssets.AssetName = @AssetName";
connection.Open();
command.Parameters.Add("@AssetName",SqlDbType.NVarchar).Value = Aname;
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
TextBoxAssetID.Text = reader["AssetID"].ToString();
TextBoxDomain.Text = reader["Domain"].ToString();
TextBoxOS.Text = reader["OSname"].ToString();
TextBoxSP.Text = reader["SP"].ToString();
TextBoxMemory.Text = reader["Memory"].ToString();
TextBoxProcessor.Text = reader["Processor"].ToString();
TextBoxManufacturer.Text = reader["Manufacturer"].ToString();
TextBoxModel.Text = reader["Model"].ToString();
}
////// Rest of code goes from Here ...
Upvotes: 0