Question

In: Computer Science

I've created a C# Windows form application in Visual stuidos it allows the user to enter...

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();

}
}
}

Solutions

Expert Solution

Application Name :Reports

Type of Application :C#

IDE Used :Visula Studio 2019

Langauge Used :C#

NOTE :

  • HERE A METHOD WITH NAME bindGrid() CREATED WHICH FULL THE RECORDS FROM THE TABLE AND SHOW IN THE GRIDVIEW.
  • THIS METHOD IS CALLED ON FORM LOAD EVENT AND AFTER THE DATA HAS BEEN INSERTED INTO THE DATABASE.

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


Related Solutions

A, B:   Design and Implement a C# windows form application to ask the user for 10...
A, B:   Design and Implement a C# windows form application to ask the user for 10 integer numbers, sort them in ascending order and display the sorted list. Use bubble sort technique to sort the array elements and do not use any built-in sort method to sort the array elements.                                                        [02] C:    Test and evaluate your program by inputting variety of values.
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
Write a C++ console application that allows your user to enter the total rainfall for each...
Write a C++ console application that allows your user to enter the total rainfall for each of 12 months into an array of doubles. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts.
Write an application that allows a user to enter the names and birthdates of up to...
Write an application that allows a user to enter the names and birthdates of up to 10 friends. Continue to prompt the user for names and birthdates until the user enters the sentinel value ZZZ for a name or has entered 10 names, whichever comes first. When the user is finished entering names, produce a count of how many names were entered, and then display the names. In a loop, continuously ask the user to type one of the names...
Show: Create an application that allows the user to enter the number of calories and fat...
Show: Create an application that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *9 The percentage of...
Write a Windows Form application named SumFiveInts. Microsoft Visual C#: An Introduction to Object-Oriented Programming,7th Edition....
Write a Windows Form application named SumFiveInts. Microsoft Visual C#: An Introduction to Object-Oriented Programming,7th Edition. Ch. 5, page 220. Take snip of program results.
Program on Visual Basic, VBA Create an application that lets the user enter a number of...
Program on Visual Basic, VBA Create an application that lets the user enter a number of seconds and produces output according to the following criteria: There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
Using PHP, Make a form that allows the user to enter the weight of the item...
Using PHP, Make a form that allows the user to enter the weight of the item being shipped. This will be used to calculate the shipping cost.Create a form that uses the method POST The form should capture a customer's package weight with the one field for the package weight in pounds. All fields should have the REQUIRED attribute. The form should have a submit button and a reset button. The form should look nice. All the labels should line...
Must be in Visual C# using windows forms : Create an application that opens a specified...
Must be in Visual C# using windows forms : Create an application that opens a specified text file and then displays a list of all the unique words found in the file. Hint: Use a LINQ method to remove all duplicate words.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT