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

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>

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 .

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

Can't see parameters using dataset with crystal reports

I've been dealing with this problem for more than 6 months, when I create a dataset and inside de dataset I call a stored procedure and then call this dataset into my report (crystal reports) I can't see the parameter fields:
I try to create the parameters in a sqlcommand and then add the parameters to my report using
rpt.setParameters("#a","sample");
but I got an error.
What could I do? please help me, I'm very desperated and angustied with this
My code (Sorry if it's in spanish) :
Dim cmd As New SqlCommand("prd_generarReporteOP", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#idop", SqlDbType.Int).Value = CInt(Session("idop"))
cmd.Parameters.Add("#producto", SqlDbType.VarChar).Value = ""
Dim da As New SqlDataAdapter da.SelectCommand = cmd
Dim ds As New DataSetOP da.Fill(ds, "DataSetOP")
You need to change your data source. Since you using the the dataset as the datasource you already getting the filtered results.
You need to use the stored-procedure as your data source and then the parameters will appear

Grid view with Datasource control

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

SQL Server 2005

i created one ASP.Net project using SQL Server 2005 .I successfully inserted .But i dont know to view the table.Please tell me
A beginner instrudciton into SQL server seems in order - you seem to stumble around not knowing anything about hwat you really deal with.
I suggest heading over to ASP.NET (http://www.asp.net/) and read the indroduction documentation, as well as have some look at the sample code and the beginner forum.
Well this is a pretty broad question, but to get data into a datatable you could do something like:
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("Retrieve", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#ID", SqlDbType.Int).Value = ID;
SqlDataAdapter sqldata = new SqlDataAdapter(cmd);
con.Open();
sqldata.Fill(dt);
con.Close();

Resources