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.
Related
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?
I am having trouble with my SQL statement to pass the value from my dropdownlist select to my gridview. I tested my SQL statement Serve Management Studio, with a specific date, and it works, but it isn't working with select value from my Dropdownlist. How would I pass the value to the Gridview? Thank you for help, I am new student to the asp.net world.
Image of web application:
Image of My DATABASE:
public void RefreshDay()
{
SqlConnection conn = new SqlConnection(#"data source =.\sqlexpress; integrated security = true; database = DBdentist");
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
string sqlsel = "SELECT Distinct patient.patientID, day from patient, patientreservation where patient.patientID = patientreservation.patientID";
try
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sqlsel, conn);
ds = new DataSet();
da.Fill(ds, "myDay");
dt = ds.Tables["myDay"];
DropDownListDay.DataSource = dt;
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "patientID";
DropDownListDay.DataBind();
DropDownListDay.Items.Insert(0, "Select Day");
}
catch (Exception ex)
{
LabelDay.Text = ex.Message;
}
finally
{
conn.Close();
}
}
protected void DropDownListDay_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownListDay.SelectedIndex != 0)
{
SqlConnection conn = new SqlConnection(#"data source =.\sqlexpress; integrated security = true; database = DbDentist");
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
string sqlsel = "SELECT patientreservation.patientID, patient.firstname, patient.lastname, patientreservation.day, patientreservation.hour, treatment.treatment FROM((patientreservation INNER JOIN patient ON patientreservation.patientID = patient.patientID) INNER JOIN treatment ON patientreservation.treatmentID = treatment.treatmentID) WHERE patientreservation.day = " + DropDownListDay.SelectedValue + "";
try
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sqlsel, conn);
ds = new DataSet();
da.Fill(ds, "myDay");
dt = ds.Tables["myDay"];
GridViewDay.DataSource = dt;
GridViewDay.DataBind();
}
catch (Exception ex)
{
LabelDay.Text = ex.Message;
}
finally
{
conn.Close();
}
}
else
{
LabelDay.Text = "You choose None:";
}
}
}
}
You can simply use this:
WHERE patientreservation.day = '" + DropDownListDay.Text.ToString() + "'";
Thanks for the help. I tried everything
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "day"
helped. I was also having some problem with my gridview. I added up deleting it and making a new, somehow that help.
The configuration of your dropdown is
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "patientID";
but you need the day as a value if you want to filter with the current selected value
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "day";
SELECT patientreservation.patientID, patient.firstname, patient.lastname, patientreservation.day, patientreservation.hour, treatment.treatment FROM((patientreservation INNER JOIN patient ON patientreservation.patientID = patient.patientID) INNER JOIN treatment ON patientreservation.treatmentID = treatment.treatmentID) WHERE **patientreservation.day** = " + DropDownListDay.**SelectedValue** + "";
Or you can try with WHERE **patientreservation.day** = " + DropDownListDay.**SelectedText** + "";
The dropdownlist is not triggering the event. Try to put this AutoPostBack="true" in your asp dropdownlist tag.
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.
Currently i'm populating a combobox with items from a database.However, i want the first item of combobox to be "---Select---".I am using the following code.All the items from database are getting populated but not the item "---Select---". Any help in this regard is highly appreciated.
private void popClass()
{
cmbClass.Items.Clear();
DataSet ds = new DataSet();
cmbClass.Items.Add("---Select----");
string sqlPS = #"SELECT * FROM tblclass_msb";
try
{
using (FbConnection conPS = new FbConnection(connectionString))
{
conPS.Open();
using (FbCommand cmdPS = new FbCommand(sqlPS, conPS))
{
using (FbDataAdapter da = new FbDataAdapter())
{
da.SelectCommand = cmdPS;
da.Fill(ds);
cmbClass.DataSource = ds.Tables[0];
cmbClass.ValueMember = "c_id";
cmbClass.DisplayMember = "c_name";
}
}
}
}
catch (FbException ex)
{
MessageBox.Show("PC-->>" + ex.Message);
}
}
i have tried other solutions mentioned somewhere in the other threads but its not working for me
you have to add new row at zero position you an do it like this:
private void popClass()
{
cmbClass.Items.Clear();
DataSet ds = new DataSet();
cmbClass.Items.Add("---Select----");
string sqlPS = #"SELECT * FROM tblclass_msb";
try
{
using (FbConnection conPS = new FbConnection(connectionString))
{
conPS.Open();
using (FbCommand cmdPS = new FbCommand(sqlPS, conPS))
{
using (FbDataAdapter da = new FbDataAdapter())
{
da.SelectCommand = cmdPS;
da.Fill(ds);
DataRow dr=ds.Tables[0].NewRow();
dr["c_id"]=0;
dr["c_name"]="--Select--";
ds.Tables[0].Rows.InsertAt( dr,0);
cmbClass.DataSource = ds.Tables[0];
cmbClass.ValueMember = "c_id";
cmbClass.DisplayMember = "c_name";
}
}
}
}
catch (FbException ex)
{
MessageBox.Show("PC-->>" + ex.Message);
}
}
If you were connecting to SQL Server using SqlCommand, you could simply change the SQL statement to the following:
string sqlPS = #"select 0 as c_id, '--Select--' as c_name union SELECT c_id, c_name FROM tblclass_msb";
However, it looks like you're connecting to Firebird and I can't be sure if union is supported.
I need some help to complete this. I have searched and tried several ways but is not enetring in my head yet. It is not homework! I am a self learner.
I managed to populate a grid selecting the table from a dropdown:
private void radDropDownList1_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
if (radDropDownList1.SelectedIndex > 0)
{
radGridView1.Visible = true;
label8.Text = radDropDownList1.SelectedValue.ToString();
DataTable dt = new DataTable();
var selectedTable = radDropDownList1.SelectedValue; //Pass in the table name
string query = #"SELECT * FROM " + selectedTable;
using (var cn = new SqlConnection(connString))
{
cn.Open();
try
{
SqlCommand cmd = new SqlCommand(query, cn);
using (var da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
catch (SqlException ex)
{
//MessageBox.Show(ex.StackTrace);
}
}
radGridView1.DataSource = dt;
}
Now I am trying to write the event to update the sql table using the grid as datasource:
private void radGridView1_CellEndEdit(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
DataTable dt = (DataTable)radGridView1.DataSource;
DataSet ds = new DataSet();
ds.Tables[0] = dt;
try
{
//**I am lost here!**
}
catch
{
}
}
Could you please help? How can I now update the sql table?
I don't really like the way you are managing the updates but... this code here will send an update statement:
using (var cn = new SqlConnection(connString))
{
cn.Open();
try
{
SqlCommand cmd = new SqlCommand("UPDATE YourTable SET Column = #Parm1 WHERE Col = #Parm2", cn);
var parm1 = cmd.CreateParameter("Parm1");
parm1.Value = "SomeValue";
var parm2 = cmd.CreateParameter("Parm2");
parm2.Value = "SomeOtherValue";
cmd.Parameters.Add(parm1);
cmd.Parameters.Add(parm2);
int rowsAffected = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
//MessageBox.Show(ex.StackTrace);
}
}