Select from two tables by using a textbox value - c#

I have two tables and I want to search into them by using a TextBox and this is my code but wrong hope to help me
string constring = "Data Source =.; initial Catalog = business; Integrated Security=SSPI;";
SqlConnection CN = new SqlConnection(constring);
DataTable dt = new DataTable();
if (txtID.Text.Trim() != "")
{
SqlDataAdapter sda = new SqlDataAdapter("select tab1.ID ,tab1.DATMOSTAND ,tab1.MONY ,tab2.BYAN ,tab2.MONY from MAL_ERTEBAT,tab2 where tab1.ID = tab2.EID = '" + txtID.Text + "'", CN);
sda.Fill(dt);
}
dataGridView1.DataSource = dt;
where tab1.ID = tab2.EID = '" + txtID.Text + "'" this is the error part the message is "in correct syntax near ="

Hope you are missing the alias name:
Replace the above SqlDataAdapter with:
SqlDataAdapter sda = new SqlDataAdapter("select tab1.ID ,tab1.DATMOSTAND ,tab1.MONY ,tab2.BYAN ,tab2.MONY from MAL_ERTEBAT tab1,tab2 where tab1.ID = tab2.EID = '" + txtID.Text + "'", CN);

Related

c# asp.net check if column in database contains string

In my database, the column is stored as 'Here is jurong', I want to read out the database row if the column contains the word jurong.
I tried using like but it cant work
Here is the code, please help
cmd = new SqlCommand("Select * from Thread WHERE (Delete_Status='No') AND (Thread_Location = '" + searchTB.Text + "' OR Thread_Title = '" + searchTB.Text + "' OR Thread_Description LIKE '%' + #Thread_Description + '%') ORDER BY ThreadID DESC ", con);
cmd.Parameters.Add("#Thread_Description", SqlDbType.VarChar).Value = searchTB.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
reptater.DataSource = ds;
reptater.DataBind();
Does that SQL statement return rows when you run it in the database? Also, I'd be careful of SQL injection attacks when passing values not through parameters.
Should your 'reptater' bind to a DataTable rather than a DataSet?
cmd = new SqlCommand("SELECT * FROM Thread WHERE (Delete_Status='No') AND (Thread_Location = #SearchTxt OR Thread_Title = #SearchTxt OR Thread_Description LIKE '%' + #SearchTxt + '%') ORDER BY ThreadID DESC ", con);
cmd.Parameters.Add("#SearchTxt", SqlDbType.VarChar);
cmd.Parameters["#SearchTxt"].Value = searchTB.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
reptater.DataSource = ds; //ds.Tables[0]; ?
reptater.DataBind();

how to convert dataAndTimePicker value to a string in c#

I need help, I am trying to write a query string that gets data according to a particular date picked in the dataAndTimePicker tool but I keep getting the same exception "Conversion failed when converting date and/or time from character string."
this is the code I tried
str = "select imId,imCustomer,imAmount,imDiscount,imTotal,imPaid,imPayType,imDate from invoiceMaster where imDate >= '" + startDate.Value.ToString("dd/MM/yyyy") + "' and imDate <= '" + endDate.Value.ToString("dd/MM/yyyy") + "'";
SqlDataAdapter da = new SqlDataAdapter(str, declerations.con);
DataTable dt = new DataTable();
da.Fill(dt);
dgvInvoice.DataSource = dt;
Always use parameters instead of putting a values directly into a sql string
str = "select imId,imCustomer,imAmount,imDiscount,imTotal,imPaid,imPayType,imDate from invoiceMaster where imDate >= #StartDate and imDate <= #EndDate";
SqlCommand cmd = new SqlCommand(str, declarations.con);
cmd.Parameters.AddWithValue("#StartDate", startDate.Value);
cmd.Parameters.AddWithValue("#EndDate", endDate.Value);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dgvInvoice.DataSource = dt;
str = "select imId,imCustomer,imAmount,imDiscount,imTotal,imPaid,imPayType,imDate from invoiceMaster where imDate >= '" + Convert.ToDateTime(startDate.Value) + "' and imDate <= '" + Convert.ToDateTime(endDate.Value) + "'";
SqlDataAdapter da = new SqlDataAdapter(str, declerations.con);
DataTable dt = new DataTable();
da.Fill(dt);
dgvInvoice.DataSource = dt;

Sql Exception was unhandled c# log in

SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename= C: \Users\Marco\Documents\Visual Studio 2015\Projects\HungryGorilla\HungryGorilla\User.mdf;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from [Login] where Username='" + unametxt.Text + "'and Password = '" + pwordtxt.Text + "'",con);
DataTable dt = new DataTable();
sda.Fill(dt);
the SDA has error = SqlException was unhandled

Is there anyway to decrypt data from database while showing it on a datagridview

string db = #" Data Source = Adeed\SQLEXPRESS; Initial Catalog = TMS; Integrated Security = True";
SqlConnection con = new SqlConnection(db);
string dgquery;
// query check if start break null then calculate hours without breaks
dgquery = ("SELECT * FROM [HDMS].[DBO].[pnt_reg_info] WHERE reg_date BETWEEN '" + dateTimePicker1.Value.ToString("MM/dd/yyyy") + "' and '" + dateTimePicker2.Value.ToString("MM/dd/yyyy") + "' and Active = '0' order by pnt_id");
SqlCommand cmdDataBase = new SqlCommand(dgquery, con);
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

how to show emp leave balance in textbox1 based on leavetype selection

I want to show emp leave balance in textbox1 based on leavetype and emp id.
I'm using below code but it's not showing total leave balance.
private void cbleavetype_SelectedIndexChanged()
{
conn = new SqlConnection(#"Data Source=........")
conn.Open();
SqlCommand command = new SqlCommand("select leave from leaveallotment where id='" + comboBox1.Text + "' && leavetype='" + cbleavetype.Text + "'",conn);
SqlDataAdapter adp = new SqlDataAdapter(command);
DataTable tbl = new DataTable();
adp.Fill(tbl);
// Show total leave in textBox1
textBox1.Text = cbleavetype.SelectedValue.ToString();
}
I think one thing that needs to happen is to change '&&' to 'and'
You can use this below code snipset,
connectionString.Open();
SqlCommand command =
new SqlCommand(
"select LeaveCount from leaveallotment where
id= '" + comboBox1.Text + "' and
leavetype='" + comboBox2.Text + "'",
connectionString);
SqlDataReader da = command.ExecuteReader();
if (da.Read()) {
textBox1.Text = da["LeaveCount"].ToString();
}
connectionString.Close();

Categories