C# Oracle SQL Parameter binding to variable? - c#

I want to bind a variable in a c# oracle SQL parameter. But it doesn't bind. I checked the return from the messagebox, but I still can't get the variable value. If you comment the adapter part, an error appears.
string sql = "SELECT * FROM ITSTEMP WHERE TO_CHAR(WEEK,'YYYYMMDD') = :DTTM";
OracleDataAdapter adapter = new OracleDataAdapter(sql, DBHelper.DBConn);
DataTable date_table = new DataTable();
adapter.SelectCommand.Parameters.Add(new OracleParameter("DTTM", dateTimePicker3.Text));
MessageBox.Show(sql);
try
{
adapter.Fill(date_table);
dataGridView1.DataSource = date_table;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
enter image description here
string sql = "SELECT * FROM ITSTEMP WHERE TO_CHAR(WEEK,'YYYYMMDD') = :DTTM";
OracleDataAdapter adapter = new OracleDataAdapter(sql, DBHelper.DBConn);
DataTable date_table = new DataTable();
//adapter.SelectCommand.Parameters.Add(new OracleParameter("DTTM", dateTimePicker3.Text));
MessageBox.Show(sql);
try
{
adapter.Fill(date_table);
dataGridView1.DataSource = date_table;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
enter image description here
How do I bind?

Related

Retrieving image path from database into datagridview

private void kayitlarilistele()
{
try
{
dataGridView1.AutoGenerateColumns = false;
baglantim.Open();
SqlDataAdapter listele = new SqlDataAdapter ("select * from Reaksiyon_Model", baglantim);
DataSet dshafiza = new DataSet();
listele.Fill(dshafiza);
dataGridView1.DataSource = dshafiza.Tables[0];
dataGridView1.Columns["Tanim"].DataPropertyName = "Tanim";
dataGridView1.Columns["NeZaman"].DataPropertyName= "Reaksiyon_NeZaman";
dataGridView1.Columns["Görsel"].DataPropertyName = "Gorsel";
dataGridView1.Columns["ReaksiyonModeli"].DataPropertyName = "Model";
baglantim.Close();
}
catch (Exception hatamsj)
{
MessageBox.Show(hatamsj.Message);
baglantim.Close();
When I try to get image path from database i get error
invalid cast from system.string to system.drawing.image
What's wrong with my code?

C# datagridview shound not change until there is a result

Recently i developed a search function, but i want when a user inputs something on the textbox the datagridview to not go blank but instead to show the data that it already has and when the result is found than only show the result. Because now when i type anything on the search textbox the dgv immediately goes blank.
Here is my code:
private void txtBarkod_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtBarkod.Text)) {
resetTxTboxes();
}
MySqlConnection connection = Connection.prevzemiKonekcija();
try {
connection.Open();
MySqlCommand command;
MySqlDataAdapter adapter;
DataTable tabela;
string query = "SELECT * FROM artikli WHERE barcode like '%" + txtBarkod.Text + "%'";
command = new MySqlCommand(query, connection);
adapter = new MySqlDataAdapter(command);
tabela = new DataTable();
adapter.Fill(tabela);
dataGridView1.DataSource = tabela;
if (txtBarkod.Text == "") {
ShowDgV();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
}
Reset/Rebind the datasource of your datagridview only if you found something in your db.
if(tabela.Rows.Count > 0)
{
dataGridView1.DataSource = tabela;
}

SQL Query With Scalar Variable Do Not Populate The Datagridview

I have this datadridview that display data in a SQL database table. In here I have used a SQLDataAdapter and a DataTable(). Please refer the below code snippet.
private void btnSrcDataID_Click(object sender, EventArgs e)
{
try
{
dgvInsertInfo.Refresh();
SqlComm = new SqlCommand();
SqlComm.Connection = SqlConn;
SqlComm.CommandText = ("SELECT * FROM MyDataTable WHERE DataID = #SDataID");
SqlComm.Parameters.AddWithValue("#SDataID", txtDataID.Text);
SqlDataTable = new DataTable();
SqlAdapt = new SqlDataAdapter(SqlComm);
//DataSet dsQryDataId = new DataSet();
SqlAdapt.Fill(SqlDataTable);
//Passing data to DatagridView
dgvInsertInfo.DataSource = SqlDataTable;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I think the issue is with either the SQL QueryString or SqlAdapt.Fill(), but I cannot understand the issue. Could you please someone help me on this.
Thanks,
Chiranthaka
Actually in my case the error occurred due to a typo error when passing the value to the Scalar variable #SDataID. So that the SQL statement's syntax is correct. Please refer the correct SQL statement below.
private void btnSrcDataID_Click(object sender, EventArgs e)
{
try
{
dgvInsertInfo.Refresh();
SqlComm = new SqlCommand();
SqlComm.Connection = SqlConn;
SqlComm.CommandText = "SELECT * FROM MyDataTable WHERE (DataID LIKE #SDataID)";
SqlComm.Parameters.AddWithValue("#SDataID", txtSrcDataID.Text);
SqlDataTable = new DataTable();
SqlAdapt = new SqlDataAdapter(SqlComm);
SqlAdapt.Fill(SqlDataTable);
dgvInsertInfo.DataSource = SqlDataTable;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Now the data is populating correctly in the DataGridView.
Thanks.

Firebird results to Gridview

I am trying to load the firebird results into asp.net gridview but it does not load anything.
What am I doing wrong: All Im trying to do is to run a SP in firebrid, send a parameter and show the results in GridView.
public void BindGridview()
{
try
{
transportFbConn.Open();
if (transportFbConn.State == ConnectionState.Closed)
{
transportFbConn.Open();
}
FbTransaction ft = transportFbConn.BeginTransaction();
transportFbCommand = new FbCommand("EXECUTE PROCEDURE SPB_PNM_SO_HIST(#PNM)", transportFbConn, ft);
transportFbCommand.CommandType = CommandType.StoredProcedure;
transportFbCommand.Parameters.Add("#PNM", FirebirdSql.Data.FirebirdClient.FbDbType.Integer, 9999999, "PNM_AUTO_KEY").Direction = ParameterDirection.Input;
transportFbCommand.Parameters[0].Value = Convert.ToInt32(Server.HtmlEncode(this.TextBox1.Text));
FbDataAdapter da = new FbDataAdapter(transportFbCommand);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
this.ErrorLabel.Text = (ex.Message);
}
finally
{
transportFbConn.Close();
}
}
Mark's reponce helped me and I used Select * form Store Procedure (#parameter) instead.

CombBox is not populating the ValueMember Properly from MySQL in C#

I have two combobox in my form, using a mysql connection to a remote server. The first combobox populated nicely. However, I need the indexid since that is a foreign key to populate a second combobox. Based on the selection, it will change the data in the second combo (for the xample, i the first combo is for car makes, then the models of each make gets filled, so if I choose Nissan, the models will then have Altima, Maxima, Sentra, ... but if I chose Toyota, the combo will then show Corolla, Camry, Prius, ...)
My foreign key is always -1 for some reason. I am using the selectindex change method, but it keeps crashing/ bc the value is always -1.
I am very new to MySQL in C# and eager to learn. Any help is appreciated. The code is below.
private void cboMake_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboMake.SelectedIndex >= 0)
cboModel.Enabled = true;
else
cboModel.Enabled = false;
// get the foreign key value of the model id to get the make for each brand to populate here
// MessageBox.Show(cboModel.ValueMember);
// right now selectindex always shows -1. why?
// if it changes, you need to then enable the right cbo, else, disable.
// but also, you need the mid, fk, so you can then do a new sql statement with the where clause to populate it.
int fk = cboModel.SelectedIndex;
string connStr = "server=123.456.7.8;user=root;database=car;port=3306; password=nowayjose";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
string sql = "SELECT * FROM cs_model WHERE mid='fk'";
MySqlCommand cmd = new MySqlCommand(sql, conn);
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
cboMake.DataSource = dt;
cboMake.DisplayMember = "model";
cboMake.ValueMember = "mmid";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "MySQL Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
conn.Close();
}
private void frmMain_Load(object sender, EventArgs e)
{
string connStr = "server=123.456.7.8;user=root;database=car;port=3306; password=nowayjose";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
string sql = "SELECT * FROM cs_make";
MySqlCommand cmd = new MySqlCommand(sql, conn);
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
cboMake.DataSource = dt;
cboMake.DisplayMember = "make";
cboMake.ValueMember = "mid";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "MySQL Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
conn.Close();
}
Correct me if I'm wrong, but considering code provided, just a guess :
private void cboMake_SelectedIndexChanged(object sender, EventArgs e)
{
int fk = cboModel.SelectedIndex; // ?????
.....
}
You access here cboModel, which is not that one whom item was actually selected. At least lookin on event name cboMake_SelectedIndexChanged, seems to me that you should look on cboMake selected index.
Hope this helps.
private void cboMake_SelectedIndexChanged(object sender, EventArgs e)
{
int foreignKey = (int)cboMake.SelectedIndex +1; // i forgot to cast the int from string since the database was tinyint, it still returns string in c#
string fk = foreignKey.ToString(); // since i need the string for the query, back it goes, but i needed to do arithmetic
string connStr = "server=10.18.30.1;user=notroot;database=car;port=3306;password=password";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
string sql = "SELECT * FROM cs_model WHERE mid=" + fk; // forgot to escape out of the string (used to php $var style within double quotes)
MySqlCommand cmd = new MySqlCommand(sql, conn);
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
cboModel.DataSource = dt; // i had cboModel by a silly mistake
cboModel.DisplayMember = "model";
cboModel.ValueMember = "mmid";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "MySQL Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
conn.Close();
}

Categories