In: Computer Science
I've created a C# Windows form application in Visual stuidos it allows the user to enter information, save that information to a database, then show that information in a datagridview. Everything works fine but the datgridview. It shows that rows have been added but not the text. I can see in my database the reports that have been added so I know its alteast saving the information. I don't know what I'm doing wrong. Please help!
My code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;
namespace Reports
{
public partial class ReportsForm : Form
{
public ReportsForm()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection("Data Source=Blu;Initial
Catalog=Reports;Integrated Security=True");
SqlCommand cmd;
private void Submitbtn_Click(object sender, EventArgs e)
{
String query = ("INSERT INTO Reports_tbl (EntryDate, ReportText)
VALUES ('" + DateTime.Now + "','" + ReportsrichTextBox.Text +
"')");
con.Open();
cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Report Successfully Added");
ReportsrichTextBox.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=Blu;Initial Catalog=Reports;Integrated Security=True");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Reports_tbl", con);
DataSet ds = new DataSet();
da.Fill(ds,"Reports_tbl");
ReportsdataGridView.DataSource =
ds.Tables["Reports_tbl"].DefaultView;
con.Close();
}
}
}
Application Name :Reports
Type of Application :C#
IDE Used :Visula Studio 2019
Langauge Used :C#
NOTE :
Form1.cs[Design] :
Form1.cs :
//namespace
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;
//application namespace
namespace Reports
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//SqlConnection class object and passsing connection string to
connect to database
SqlConnection con = new SqlConnection("Data Source=.;Initial
Catalog=Blu;Integrated Security=True;Pooling=False");
SqlCommand cmd;//SqlCommand object
//Submit button click
private void Submitbtn_Click(object sender, EventArgs e)
{
//SQL query
String query = ("INSERT INTO Reports_tbl (EntryDate, ReportText)
VALUES ('" + DateTime.Now + "','" + ReportsrichTextBox.Text +
"')");
con.Open();//open the connection
cmd = new SqlCommand(query, con);//pass query and connection to
SqlCommand object
cmd.ExecuteNonQuery();//execute query
con.Close();//close the connection
//when record added into database show message
MessageBox.Show("Report Successfully Added");
//clear the textbox text
ReportsrichTextBox.Text = "";
//call method to bind the grid
bindGrid();
}
//method to bind the gridview
public void bindGrid()
{
con.Open();//open connection
//creating object of SqlDataAdapter and passing SQL query and
connection string
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Reports_tbl",
con);
//creating object of DataSet class
DataSet ds = new DataSet();
//fill the dataset
da.Fill(ds, "Reports_tbl");
//bind the datagrid view with the table from dataset
ReportsdataGridView.DataSource =
ds.Tables["Reports_tbl"].DefaultView;
con.Close();//close the connection
}
//form load event
private void Form1_Load(object sender, EventArgs e)
{
//call method to bind the grid
bindGrid();
}
}
}
===================================
Screen 1:
Screen 2:Screen when data is successfully added
Screen 3:Screen showing data from database