how to have user input and query a sql Database using c#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am having difficulties with getting rows for data. I am currently using the SQLDataAdapter to get the results and put them into a database. I figured out how to get one column but I want a row instead of just a column. How I am going to have it is if a
user inputs the primary key then it will dispaly the results of that 1 item. However, if the user inputs several different options I want to query the sql database to give me all relavant data from the selected items. For example, if someone enters the primary
key number and fills out several other data then the database will ignore all information except the primary key. However, if someone inputs the serial number, cost (with a feature to select a range that is equal to greater than or less than the amount typed),
and owner name, then I want to display everything by that owner with that serial number and with the cost (or range of cost). But what I want is for the table to display the entire row of everything that has those particular items in it (like a filter). So
items 1-4 and 7 all have Harddrive stored in the items column, I want the table to display only rows 1-4 and 7 (everything in them like the owner, cost, etc.).
Here is my code now, it just displays the first column when you enter the data:
<pre> private void SearchButton_Click(object sender, EventArgs e)

{

//TODO change this to add more features

string numberCheck = InputTextBox.Text.Trim();

string costCheck = LookUpCostTextBox.Text.Trim();

string sortByCost = LookUpCostComboBox.Text;



bool isNotNumber = true;



for (int i = 0; i < numberCheck.Length; i++)

{

if (!char.IsNumber(numberCheck))

{

isNotNumber = true;

break;

}

else

{

isNotNumber = false;

}

}

// verify that cost is valid

for (int i = 0; i < costCheck.Length; i++)

{

if (!char.IsNumber(costCheck))

{

isNotNumber = true;

break;

}

else

{

isNotNumber = false;

}

}



if (numberCheck.Trim() != "" && isNotNumber == true)

{

MessageBox.Show("You must enter a valid integer", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);



}

else if (costCheck.Trim() != "" && isNotNumber == true)

{

MessageBox.Show("You must enter a valid integer", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

else

{

// close the form and return to the previous event handler with ID being changed

LookUpForm.Close();

string myRequest = "SELECT ";



string serialNumber = LookUpSerialTextBox.Text;

string partNumber = LookUpPartNumberTextBox.Text;

string otherNumber = LookUpOtherNumberTextBox.Text;

string generation = LookUpGenerationComboBox.Text;

string owner = LookUpOwnerComboBox.Text;

string productCategory = LookUpItemComboBox.Text;



int cost = 0;

int gdrNumber = 0;

if (InputTextBox.Text.Trim() != "")

gdrNumber = Convert.ToInt32(InputTextBox.Text);

if (LookUpCostTextBox.Text.Trim() != "")

cost = Convert.ToInt32(LookUpCostTextBox.Text);



if (InputTextBox.Text.Trim() != "")

myRequest += "GDR_ID#[" + gdrNumber + "] from Items";









string myConn = "Persist Security Info=False; User ID=joe;Initial Catalog=Inventory;" +

"Password=inventory;" +

"Data Source=vmspdgdr001.amr.corp.intel.com";



try

{

SqlConnection myConnection = new SqlConnection(myConn);

SqlCommand myQuery = new SqlCommand(myRequest, myConnection);

SqlDataAdapter dataAdapter = new SqlDataAdapter(myQuery);

DataTable table = new DataTable();

dataAdapter.Fill(table);

OutputTable.DataSource = new BindingSource(table, null);



}

catch (Exception ie)

{

MessageBox.Show(ie.Message);

}

OutputTable.RowHeadersVisible = false;

Table.ShowDialog();

}

}

[/code]
I know there are some issues though I am not sure how you only select a row instead of columns, any ideas?

<br/>

View the full article
 
Back
Top