simple gridview with dataset error - c#

In a simple datagridview it gives me error when run (Compilation Error):(Source Error:
Line 11:
Line 12:
Line 13: )
mycode
public partial class website : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Fillgrid();
}
public void Fillgrid()
{
Dataset ds = new Dataset();
SqlConnection cn = new SqlConnection(#"Server=.\SQLEXPRESS; DataBase=first; Integrated Security=true;");
cn.Open();
SqlCommand cmd;
cmd = new SqlCommand("select * from attendance", cn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
cn.Close();
selectGridView.DataSource = ds;
selectGridView.DataBind();
}
}

I think you can use the following code!!
Dataset ds = new Dataset();
SqlConnection cn = new SqlConnection(#"Server=.\SQLEXPRESS; DataBase=first; Integrated Security=true;");
cn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select * from attendance", cn);
adapter.Fill(ds,"attendance");
cn.Close();
selectGridView.DataSource = ds;
selectGridView.DataBind();

Related

how to fix SQL syntax error...upto...for the right syntax to use near '())' at line 1

i am getting error in I am getting error in da.Fill(ds) . i want to check CNIC before saving the form . if it exists then form should be submitted otherwise it should say , CNIC is not valid.
public partial class ReportCrime : System.Web.UI.Page
{
MySqlConnection con = new MySqlConnection();
MySqlCommand cmd = new MySqlCommand();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=localhost;Initial Catalog=fir;Integrated Security=True;User Name=root;Password=;";
con.Open();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
MySqlCommand cmd = new MySqlCommand("select count(CNIC) from reportcrime where CNIC=(txtCnicNo.Text.ToString())", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if (i > 0)
{
MySqlCommand acmd = new MySqlCommand("insert into reportcrime" + "(Name,CNIC,Phone1,Phone2,Email,Gender,City,Address,CrimeType,CrimeDetail)values(#Name,#CNICNO,#ContactNo.1,#ContactNo.2,#EmailAddress,#Gender,#CityName,#PostalAddress,#CrimeType,#CrimeDetail)", con);
acmd.Parameters.AddWithValue("#Name", txtName.Text);
acmd.Parameters.AddWithValue("#CNICNO", txtCnicNo.Text.ToString());
acmd.Parameters.AddWithValue("#ContactNo.1", txtcontactNo1.Text);
acmd.Parameters.AddWithValue("#ContactNo.2", txtcontactNo2.Text);
acmd.Parameters.AddWithValue("#EmailAddress", txtEmail.Text);
acmd.Parameters.AddWithValue("#Gender", ddlGender.SelectedItem.Value);
acmd.Parameters.AddWithValue("#CityName", txtCityName.Text);
acmd.Parameters.AddWithValue("#PostalAddress", txtaddress.Text);
acmd.Parameters.AddWithValue("#CrimeType", ddlCrimeType.SelectedItem.Value);
acmd.Parameters.AddWithValue("#CrimeDetail", txtCrimeDetails.Text);
acmd.ExecuteNonQuery();
}
else
{
MessageBox.Show("CNIC is not Valid");
}
}
}
if the value passing from text box is just string then this should work
MySqlCommand cmd = new MySqlCommand("select count(CNIC) from reportcrime where CNIC='"+Convert.ToString(txtCnicNo.Text)+"'", con);

how to use 2 DataGridViews in one form (c#)?

I want to use two DataGridViews in this form, which will receive their information from two different tables in one database. But when I run the program, both DataGridViews only display the second table information.
private void Ring_Load(object sender, EventArgs e)
{
showdata();
showmedal();
}
void showdata()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
da.SelectCommand = cmd;
dt.Clear();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns[3].Visible = false;
}
void showmedal()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
da.SelectCommand = cmd;
dt.Clear();
da.Fill(dt);
dataGridView2.DataSource = dt;
}
private void Ring_Load(object sender, EventArgs e)
{
showdata();
showmedal();
}
void showdata()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
da.SelectCommand = cmd;
using(DataTable dt = new DataTable())
{
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.DataBind();
dataGridView1.Columns[3].Visible = false;
}
}
void showmedal()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
da.SelectCommand = cmd;
using(DataTable dt = new DataTable())
{
da.Fill(dt);
dataGridView2.DataSource = dt;
dataGridView2.DataBind();
}
}
Try this
private void Ring_Load(object sender, EventArgs e)
{
showdata();
showmedal();
}
void showdata()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
da.SelectCommand = cmd;
dt.Clear();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns[3].Visible = false;
}
void showmedal()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
da.SelectCommand = cmd;
dt.Clear();
dt = new DataTable();
da.Fill(dt);
dataGridView2.DataSource = dt;
}
You seem to be reusing da and dt. Reusing da is no problem, but reusing dt is. When you assign dt to DataGridView.DataSource, the data is NOT copied! So in the end, both DataGridViews will be using the same DataTable object, that holds the data from the second table (medal).
You could try this:
private void Ring_Load(object sender, EventArgs e)
{
showdata();
showmedal();
}
void showdata()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
da.SelectCommand = cmd;
DataTable dt1 = new DataTable();
da.Fill(dt1);
dataGridView1.DataSource = dt1;
dataGridView1.Columns[3].Visible = false;
}
void showmedal()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
da.SelectCommand = cmd;
DataTable dt2 = new DataTable();
da.Fill(dt2);
dataGridView2.DataSource = dt2;
}
Edit: renamed the local DataTable variables for clarity.

how to search data in db

This code is working fine but when i search id like 121452, so it will call all numbers that have "1" or "2" in db. so i want it show only the exact id what i search
private void btn_search_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\acap\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30");
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Student where No_ic = " + boxSearch.Text, con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
//dataGridView1.Visible = true;
}
Try this :
Updated: (Previously i forgot to bind dataset.)
private void btn_search_Click(object sender, EventArgs e) {
String bResult = boxSearch.Text;
string connectionString = "Data Source=.;Initial Catalog=sacbase;Integrated Security=True"; // add your conncetion string here
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM Student where No_ic =#val", connection);
cmd.Parameters.AddWithValue("#val", bResult);
SqlDataAdapter dataadapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "student_table");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "student_table";
}

Assign query result into a session

i want assign result into a session
c# code
protected void BindData()
{
SqlConnection con = new SqlConnection(#"Data Source=DESKTOP-677TN4G\SQLEXPRESS;Initial Catalog=homework;Persist Security Info=True;User ID=sa;Password=123456");
DataSet ds = new DataSet();
DataTable FromTable = new DataTable();
con.Open();
string cmdstr = "Select CourseName from Staff where FacultyNumber=#idd";
SqlCommand cmd = new SqlCommand(cmdstr, con);
cmd.Parameters.AddWithValue("#idd", Session["id"].ToString());
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
so how i can do it i try to do
Session["id"] = cmdstr.Text;
but it not work
i do it like this but not work
I do it like this
protected void BindData()
{
SqlConnection con = new SqlConnection(#"Data Source=DESKTOP-677TN4G\SQLEXPRESS;Initial Catalog=homework;Persist Security Info=True;User ID=sa;Password=123456");
DataSet ds = new DataSet();
DataTable FromTable = new DataTable();
con.Open();
string cmdstr = "Select CourseName from Staff where FacultyNumber=#idd";
SqlCommand cmd = new SqlCommand(cmdstr, con);
cmd.Parameters.AddWithValue("#idd", Session["id"].ToString());
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
DataList1.DataSource = ds.Tables[0];
Session.Add("Staff", ds.Tables[0]);
DataList1.DataBind();
Label1.Visible = true;
Label1.Text = "Course Name is : " + Session["Staff"].ToString();
}
and the output is
Course Name is : Table
not the selected value
In your case the result is a DataTable no problem you can store this DataTable to the session as well. Let staffData be the DataTable that you are populating from the database,
Session.Add("Staff", staffData); // Adding datatable to the session
In your case you are using ds is used as a DataSet and you are accessing ds.Tables[0] to bind the grid. in this case you can use something like: Session.Add("Staff", ds.Tables[0]);.
Later you may come up with another question that how can I retrieve this DataTable from the session, Here I'm adding the answer for that as well.
DataList1.DataSource = (DataTable)Session["Staff"];

How can I display the latest value in Chart?

I have to display chart that is connected with db dynamically. It is working fine. The code is given below. But my issue is my db is automatically update in every 5 minutes. So I have to display the chart with latest inserted information. Please help me.
The code is given below:
public partial class chartDummy : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(#"ConnectionString");
cmd = new SqlCommand("Select Mains_Run_Hrs, DG_Run_Auto_Mode, Battery_Run_Hrs, Solar_Run_hrs from tbl_runtime_report", con);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
DataView source = new DataView(ds.Tables[0]);
Chart1.DataSource = ds;
Chart1.Series[0].YValueMembers = "Mains_Run_Hrs";
Chart1.Series[0].XValueMember = "DG_Run_Auto_Mode";
Chart1.Series[0].XValueMember = "Battery_Run_Hrs";
Chart1.Series[0].XValueMember = "Solar_Run_hrs";
Chart1.DataBind();
}
}
cmd = new SqlCommand("Select Mains_Run_Hrs, DG_Run_Auto_Mode, Battery_Run_Hrs, Solar_Run_hrs from tbl_runtime_report ORDER BY Site_ID DESC", con);

Categories

Resources