Here is my code to set data on my crystal report but data is not comin in data set but with the same time dataset is fill for datagridview. please help me. thanks in advance
private void button1_Click(object sender, EventArgs e)
{
ReportDocument crystalrpt = new ReportDocument();
crystalrpt.Load(#"E:\c#\Date_day\Date_day\CR1.rpt");
Rst_PrntDataSet prnt = Getdata("select * from dbo.EMPL_TRN");
crystalrpt.SetDataSource(prnt);
CRV1.ReportSource = crystalrpt;
CRV1.Refresh();
}
private Rst_PrntDataSet Getdata(string qry)
{
string cs = ConfigurationManager.ConnectionStrings["Rst"].ConnectionString;
SqlCommand cmd = new SqlCommand(qry);
using (SqlConnection con = new SqlConnection(cs))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.Connection = con;
da.SelectCommand = cmd;
using (Rst_PrntDataSet prnt = new Rst_PrntDataSet())
{
da.Fill(prnt, "tbl1");
return prnt;
}
}
}
}
Are you change any db object after creating this report i.e. your report is upto date with database.
If yes, then please check in result in design mode by passing value (just F5 to view result).
If no, then simple go to "Database" menu -> "update database" and follow the step as below link
http://www.softwareforces.com/Support/Learning-Center/Step-by-Step/rpt-Inspector/Changing-Data-source-and-Database-Crystal-Reports-Dev-to-QA-to-Production
Related
I was currently working on a project and wants to load the called data on the datagridview upon form_load, But I wasn't sure why the supposed data wasn't loading. Attached are my codes, could you help me pinpoint whats wrong with this? Needed it badlly.
Code:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = GetV9_data();
}
private DataTable GetV9_data()
{
DataTable V9db = new DataTable();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source = 10.47.48.80; Initial Catalog = V9_Hirata_L2; User ID = Guest; Password = ************;";
SqlCommand cmd = new SqlCommand("Select ID, MotorSN, StationNo, PT_NestNo, PT_TS_AW_Pin_1, PT_TimeIn FROM tbl_V9_Datalog", conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
V9db.Load(reader);
return V9db;
}
Can anyone tell me why this isn't working? I don't get an error in the code, but the information (month, day) doesn't show up in the labels. The connection is active, the data is there. It just will not display.
The listbox (listNames) is populated from the database, and that works fine. What I want is for the MONTH and DAY information from the selected record from listbox to display as labels.
Thanks for any help.
private void listNames_SelectedIndexChanged(object sender, EventArgs e)
{
using (connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM members WHERE Name = " + listNames.SelectedItem.ToString(), connection))
{
DataTable showlistTable = new DataTable();
DataSet info = new DataSet();
adapter.Fill(showlistTable);
adapter.Fill(info);
labelDetailsMonth.Text = info.Tables[0].Rows[0]["Month"].ToString();
labelDetailsDay.Text = info.Tables[0].Rows[0]["Day"].ToString();
}
}
You need single quotes around string literals in SQL (your listNames.SelectedItem value). The best way to do this is by avoiding the string literal entirely and using a query parameter. This will also fix the gaping sql injection security hole:
private void listNames_SelectedIndexChanged(object sender, EventArgs e)
{
string sql = "SELECT * FROM members WHERE Name = #Name";
using (connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connection))
{
//guessing at column type/length here
adapter.SelectCommand.Parameters.Add("#Name", SqlDbType.NVarChar, 20).Value = listNames.SelectedItem;
DataSet info = new DataSet();
adapter.Fill(info);
labelDetailsMonth.Text = info.Tables[0].Rows[0]["Month"].ToString();
labelDetailsDay.Text = info.Tables[0].Rows[0]["Day"].ToString();
}
}
I followed this tutorial https://www.aspsnippets.com/Articles/Adding-Charts-to-ASPNet-ReportViewer-Control-Display-Pie-Chart-in-RDLC-Report-in-ASPNet.aspx and this tutorial https://www.codeproject.com/articles/15597/using-the-asp-net-2-0-reportviewer-in-local-mode
Needless to say that both works very well.
The problem is that after five minutes of interaction with my wapplication, when I go back to the webforms containing the reports I get :
No data available
This happens for both reports based on those tutorial
it's just as if my datasets get unplugged after 5 min
The solution I have found so far is to RECREATE the whole report to make it work another 5 to 10 minutes. I have no idea how this can happen. Below is the code I am using :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/report1.rdlc");
DataSetArticle Myds = GetData();
ReportDataSource datasource = new ReportDataSource("dsReport", Myds .Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
}
protected DataSetArticle GetData()
{
string query = "select cola, colb from mytable";
string conString = "myconnection string";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSetArticle dsMe = new DataSet1())
{
sda.Fill(dsMe, "DataTable1");
return dsMe;
}
}
}
}
}
Below is an image of the error
Image of the debugger
I'm working on a school development project and I'm quite new to development. I have been reading online but can't find the answer I'm looking for.
So far I have created a listbox in my Windows Forms application which I want to select all the values from one of my columns, and these should work as a inparameter to display data in my dataGridView based on the parameter.
I have created 70% of my project and this functionality is what is left. My database is in Azure and I can write to it and add new rows, but I can't read anything to my application when I run it.
code for listview, at first I just want to be able to select. Later on somehow write the choosen parameter to a variable that I can use as a condition in my dataGridView.
This is the code for my gridview so far I just want to display all data in it, but it's not showing anything.
namespace MyNamespace
{
public partial class CompanyForm : Form
{
public CompanyForm()
{
InitializeComponent();
}
//Connection String
string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].
ConnectionString;
private void createCompany_Click_1(object sender, EventArgs e)
{
if (textBoxCompanyName.Text == "")
{
MessageBox.Show("Fill information");
return;
}
using (SqlConnection con = new SqlConnection(cs))
{
//Create SqlConnection
con.Open();
SqlCommand cmd = new SqlCommand
(
"insert into dbo.Company (companyName)
values(#companyName)", con);
cmd.Parameters.AddWithValue
(
"#companyName",
textBoxCompanyName.Text);
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
MessageBox.Show("GJ");
}
}
// The code that is not filling my datagrid
private void dataEmployees_Load()
{
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand
(
"Select fname,ename FROM dbo.Users", con
);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dataEmployees.DataSource = dt;
}
}
}
}
My connection string is working it's already being able to insert data to the tables that I have.
The problem why you Grid isn't shown any data is that you try to bind a SqlDataReader to it. This isn't working, because the Grid doesn't support this as DataSource.
What you need as DataSource is DataTable, IList<T>, IBindingList<T>. In your case the DataTable would be the easiest solution. Try this out:
protected void DataEmployees()
{
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand
(
"Select firstname,lastname FROM employees",con
);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dataEmployees.DataSource = dt;
}
}
Notice that Methods are written Uppercase in C#. Further notice that you don't need to close the connection manually if you use a using-block. On the end of the using-block it's automatically closed/disposed.
I have a datagridview which should contain all users in mysql database. For my problem, it adds rows based on the number of rows in the MySQL, but does not show any data on it. see image below (i have 2 rows in database):
screenshot
Here's my code that I've tried:
using MySql.Data.MySqlClient;
namespace SampleDatabaseQueries
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
MySqlConnection conn;
MySqlCommand cmd;
private void Main_Load(object sender, EventArgs e)
{
string connString = "server=localhost;user=root;password=;database=test_db";
conn = new MySqlConnection(connString);
showData();
}
public void showData()
{
string query = "SELECT * FROM test_db.users;";
cmd = new MySqlCommand(query, conn);
conn.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = cmd;
DataTable dTable = new DataTable();
adapter.Fill(dTable);
dgUsers.DataSource = dTable;
conn.Close();
}
}
}
EDIT: above code works, data was already loaded but it adds another column so I did not see at first (as you can see the long horizontal scroll on the above screenshot). To solve that, I removed the columns I manually added and changed my query to:
SELECT username AS Username, password AS Password FROM test_db.users;
Thank you!
Replace this:
MySqlDataAdapter adapter = new MySqlDataAdapter();
With:
using (MySqlDataAdapter reader = new MySqlDataAdapter(cmd))
{
adapter.Fill(dTable);
}
EDIT
For further improvement you could make a database connection class, i suppose you are using the MySql connector from oracle so you can check out their documentation on how to do this. Here is a link that can get you started on the most basic things like connecting and filling datatables: MySql connector documentation
EDIT 1
As you said my solution did not work, here is another one that I just tested and should work.
DataTable dTable = new DataTable();
conn.Open();
string query = "SELECT * FROM test_db.users;";
MysqlCommand cmd = new MySqlCommand(query, conn);
using (MySqlDataAdapter reader = new MySqlDataAdapter(cmd))
{
adapter.Fill(dTable);
}
dgUsers.DataSource = dTable;
conn.Close();
This should work. If the datatable is not showing any rows(Empty rows means it found something), then there is a different problem, maybe your database is empty.