Grid view with Datasource control - asp.net

Sir,
Im doing a project in asp.net 4.0 with c#.I my project i want to display datas from the database to the grid view using Data Source Control. But while doing it im getting an eror called "The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'System.Web.UI.WebControls.SqlDataSource' could not be found.".
My code is also givren below.
SqlCommand cmd = new SqlCommand("SPS_LeaveBalanceReport_DSO", Connect.con());
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#ReportMonth", SqlDbType.NVarChar).Value =
SelectMonthDropDown.SelectedValue.ToString();
cmd.Parameters.Add("#ReportYear", SqlDbType.NVarChar).Value =
SelectYearDropDown.SelectedItem.Text.ToString();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "tab");
GridView1.DataSourceID = SqlDataSource1.ToString();
GridView1.DataBind();
Please help me with error.,thanks In Advance.,

change
GridView1.DataSourceID = SqlDataSource1.ToString();
to
GridView1.DataSourceID = SqlDataSource1.ID;

The problem is no where in your cod ejust replace the line
GridView1.DataSourceID = SqlDataSource1.ToString();
with
GridView1.DataSource=ds;
it will work

Related

button wont save to database it comes to exception error

i made a asp.net site with 3 textboxes and 1 dropdown list and a save button evvry time i click it, it give back An exception of type 'System.NullReferenceException' occurred in Bon-Temps.dll but was not handled in user code.
it give this error on de code line
DataRow drow = ds.Tables["OpdrachtGever"].NewRow();
my question is why??
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["BT-1ConnectionString"].ConnectionString;
cnn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from OpdrachtGever";
cmd.Connection = cnn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "OpdrachtGever");
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataRow drow = ds.Tables["OpdrachtGever"].NewRow();
drow["Naam"] = TextBox1.Text;
drow["Adres"] = TextBox2.Text;
drow["PostCode"] = TextBox3.Text;
drow["AantalPersonen"] = DropDownList1.SelectedItem;
ds.Tables["OpdrachtGever"].Rows.Add(drow);
da.Update(ds, "OpdrachtGever");
You have named your table as
da.Fill(ds, " OpdrachtGever ");
^ ^
but then you refer to it with
DataRow drow = ds.Tables["OpdrachtGever"].NewRow();
without any spaces.
There is a rogue space also on this line
ds.Tables["OpdrachtGever "].Rows.Add(drow);
^
It is not a good idea to have these spaces in your table name because they are not meaningfull for us (humans) and easy to forget. But, for the indexer of the table collection, a space makes a difference. So, when you pass the name with the wrong or missing spaces, the indexer is not able to find your table and returns null .

I got exception handling at ExexuteNonQuery at gridview in asp.net

Label lb = (Label)GridView1.Rows[e.RowIndex].FindControl("Label6");
TextBox tx1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
TextBox tx2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
mycon.Open();
SqlCommand myupdatecommand = new SqlCommand("update Users set(user_name,user_surname) values('"+tx1.Text+"','"+tx2.Text+"') where user_id='"+lb.Text+"'", mycon);
myupdatecommand.ExecuteNonQuery();
GridView1.EditIndex = -1;
GridView1.DataBind();
Thanks!
first update statement is not correct, secondly please use SQL Parameters in order to avoid the SQL Injection.
SqlCommand cmd= new SqlCommand("update Users set user_name=#userName,user_surname=#userSurName where user_id=#userID", mycon);
cmd.Parameters.AddWithValue("#userName",yournameTextBox.Text);
cmd.Parameters.AddWithValue("#userSurName",yourSurnnameTextBox.Text);
cmd.Parameters.AddWithValue("#userID",yourID);

crystal reports giving Database logon failed or asking for credentials

i have tried almost everything I could. I have a web app in ASP.net with c#. I am fetching data from the database tables and adding it to dataset. Then I set this dataset as the source to the report. My code is as following.
con.ConnectionString = ConfigurationManager.ConnectionStrings["familyConnectionString"].ConnectionString;
SqlDataAdapter sda = new SqlDataAdapter("select uid, member_name, gender, dob from family where uid='"+uid+"'", con);
DataSet1 myds = new DataSet1();
sda.Fill(myds, "family");
SqlDataAdapter sda1 = new SqlDataAdapter("select id from birth_certificates where p_id='"+uid+"'", con);
sda1.Fill(myds, "birth_certificates");
ReportDocument rpt = new ReportDocument();
rpt.Load(Server.MapPath("birth_certi_report.rpt"));
rpt.Refresh();
rpt.SetDataSource(myds);
rpt.SetDatabaseLogon("","",#".\sqlexpress","project2");
CrystalReportViewer1.ReportSource = rpt;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.Visible = true;
CrystalReportViewer1.RefreshReport();
I am using integrated security so I left username and password blank. Please help.
Please Note that the above code is written inside a DropDown_selectedIndexChanged() event. I tried adding it to the page_load but it didn't work.
If you are not worried about authentication ,then do it this way.Create an instance of your crystal report and set its data source.Thats all you need to do and it works.
*Make sure the crystal report is within your solution.
con.ConnectionString = ConfigurationManager.ConnectionStrings["familyConnectionString"].ConnectionString;
// the magic
birth_certi_report myreport = new birth_certi_report();
DataSet1 myds = new DataSet1();
SqlDataAdapter sda1 = new SqlDataAdapter("select id from birth_certificates where p_id='"+uid+"'", con);
sda1.Fill(myds, "birth_certificates");
myreport.SetDataSource(myds);
crystalReportViewer1.ReportSource = myreport;
birth_certi_report myreport = new birth_certi_report();
in this line you have mentioned that birth_certi_report is crystal report. my crystal report name is applicationreport but it shows an error.
Dim myreport As New appilcationreport
error is TYPE IS NOT DEFINED

system.data.common.datarecordinternal error in .net

// Dropdown data for module_drp
SqlConnection mod_conn = new SqlConnection(#"Data Source=4D\SQLEXPRESS;Initial Catalog=tracker;Integrated Security=True");
mod_conn.Open();
string selectcmd = #"select testmodule.names from dbo.testmodule";
SqlCommand cd1 = new SqlCommand(selectcmd,mod_conn);
//cd1.ExecuteNonQuery();
SqlDataReader dr1 = cd1.ExecuteReader();
module_drp.DataSource = dr1;
module_drp.DataBind();
mod_conn.Close();
After compiling am getting an error - system.data.common.datarecordinternal
You should set the DataTextField property for the module_drp in the .aspx file with -
DataTextField="testmodule.names"
Hope this helps :).

Binding Data from A Dataset to a <asp:Table>

Here I got slapped by a nice Surprise! <asp:Table> has no DataSource property. So far I have this code.
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Careers"].ConnectionString);
conn.Open();
System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand("SELECT * FROM Careers", conn);
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(comm);
System.Data.DataSet ds = new System.Data.DataSet();
da.Fill(ds);
System.Data.DataTable dt = ds.Tables[0];
//tbVCareers.IMAGINARYDATASOURCE = dt;
tbVCareers.DataBind();
This is only a testpage I am writing , so the Table is not supposed to look pretty. How am I to bind Data to this table? Note, this is not the same database connection that I posted in an earlier question. Any insight someone can Lend to me?
<asp:Table> is not data control, so you cannot do that with <asp:Table>. you have to use <asp:GridView>

Resources