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 .
Related
I need to get back from an Oracle stored procedure (inside a package) an output parameter defined as user collection type object (a table).
Procedure GetCon (DataRif IN VARCHAR2,Results OUT my_collectiontype)
IS
BEGIN
select my_ObjectType(Col1,Col2)
bulk collect into Results
FROM
TABLE(PMS.myfunc(DataRif ,'31-12-2019');
END;
....
The Function PMS.myfunc returns a my_collectiontype object
In my code (Asp.net 4.0 VB.NET with Oracle ManagedDataAccess Drivers), I've tried
strConn = "Data Source=XE.WORK;User Id=PMS;Password=xxxx"
Dim con As New OracleConnection(strConn)
Dim dt As DataTable = New DataTable()
Dim da As OracleDataAdapter = New OracleDataAdapter()
con.Open()
Dim cmd As New OracleCommand()
cmd.Connection = con
cmd.CommandText = "MySchema.MyPackage.GetCon"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("DataRif", OracleDbType.Varchar2,"23-07-2019", ParameterDirection.Input)
cmd.Parameters.Add("Results", OracleDbType.RefCursor).Direction = ParameterDirection.Output
da.SelectCommand = cmd
cmd.ExecuteNonQuery()
da.Fill(dt)
.....
I get this error
ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'GETCONT'
ORA-06550: line 1, column 7: PL/SQL: Statement ignored"
Sure the out parameter is not a ref cursor but I can't find the right object.
I need to use a stored procedure because I know it's possible execute something like this as simple view
SELECT * FROM TABLE (PMS.myfunc('01-01-2019', '31-12-2019')
but this solution is not the best at the moment.
Thanks in advance.
Solved in this following way that permits to receive a cursor and put it in a dataset.
Create Stored Procedure in Oracle like this
CREATE OR REPLACE PROCEDURE get_emp_rs (p_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_recordset FOR
SELECT DIP_ID,DIP_NAME
FROM TABLE(fn_GetDataFunction('01-01-2019','31-12-2019','15-09-2019'));
END get_emp_rs;
/
In Asp.net
Public Function TestCursor() As DataSet
Dim strConn As String = "Data Source=MYSOURCE;User Id=MYID;Password=xxxxx"
Dim dsReturn As DataSet = Nothing
Try
Using con As New OracleConnection(strConn)
Using sda As New OracleDataAdapter()
Dim cmd As New OracleCommand()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "get_emp_rs"
cmd.Parameters.Add("p_recordset", OracleDbType.RefCursor).Direction = ParameterDirection.Output
sda.SelectCommand = cmd
Using ds As New System.Data.DataSet("results")
sda.Fill(ds, "MyTableName")
Return ds
End Using
End Using
End Using
Catch ex As Exception
'---------
End Try
End Function
How to connect to multiple databases in asp.net using SqlDataReader?
Assume that I have two databases such as “Product” and “people”. The product database has two tables, let’s say table1 and table 2, while people has two tables, let’s say again table1 and table2.
I want to get some information from Product.table1 and some from people.table2.
I tried with the following code, but unfortunately it does not work:
SqlConnection con1 = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\Product.mdf;Integrated Security=True");
SqlConnection con2 = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\People.mdf;Integrated Security=True");
SqlCommand cmd1 = new SqlCommand("select prod_name, prod_price from product_tbl", con1);
SqlCommand cmd2 = new SqlCommand("select std_name from student_tbl", con2);
con1.Open();
con2.Open();
SqlDataReader dr1 = cmd1.ExecuteReader();
SqlDataReader dr2 = cmd2.ExecuteReader();
// GridView1.DataSource = How to do it??
GridView1.DataBind();
You can either do it as follow :
Retrieve the result from Product DB in dataset1
Retrieve the result from People DB in dataset2
Use DataSet.Merge Method to merge the two data sets in a single dataset say dsProductPeople
Bind dsProductPeople to the grid
OR you can use following example :
// Assumes that customerConnection is a valid SqlConnection object.
// Assumes that orderConnection is a valid OleDbConnection object.
SqlDataAdapter custAdapter = new SqlDataAdapter(
"SELECT * FROM dbo.Customers", customerConnection);
OleDbDataAdapter ordAdapter = new OleDbDataAdapter(
"SELECT * FROM Orders", orderConnection);
DataSet customerOrders = new DataSet();
custAdapter.Fill(customerOrders, "Customers");
ordAdapter.Fill(customerOrders, "Orders");
DataRelation relation = customerOrders.Relations.Add("CustOrders",
customerOrders.Tables["Customers"].Columns["CustomerID"],
customerOrders.Tables["Orders"].Columns["CustomerID"]);
foreach (DataRow pRow in customerOrders.Tables["Customers"].Rows)
{
Console.WriteLine(pRow["CustomerID"]);
foreach (DataRow cRow in pRow.GetChildRows(relation))
Console.WriteLine("\t" + cRow["OrderID"]);
}
Using ASP.NET I need to compare data loaded from Excel file with data in SQL Server table. It's data written in cyrillic.
Basically, after script creates an array list filled with data from database table, it opens Excel file, and list data from one of the column. For every data stored in Excel cell, I have to find position in mentioned array list.
I have same data in both sources, but script returns that there are no same data at all. I guess that is something related with cyrillic letters.
Part of the code used for reading Excel cells and comparing with array list looks like this:
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
Page.Trace.Write(getExcelSheetName);
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
con.Close();
foreach (DataRow row in dtExcelRecords.Rows)
{
Response.Write(row[0]);
Response.Write(" ");
Response.Write(MyClass.areasList.IndexOf(row[0])); // always returns -1
Response.Write(Environment.NewLine);
Response.Write("<br />");
}
Can you help me solving this please? Thank you in advance!
da = New SqlDataAdapter("select * from revision_cycle_documenttype_mapping where project_id=" & Session("project_id"), con)
cb = New SqlCommandBuilder(da)
ds = New DataSet
da.Fill(ds, "revision_cycle_documenttype_mapping") <<--
mapping_count = ds.Tables("revision_cycle_documenttype_mapping").Rows.Count
Response.Write(mapping_count)
I am getting an error "Incorrect syntax near '=' ".
I have shown on which portion it's throwing error i.e da.fill.
Can you please help me?
EDIT
If it throws error on da.Fill then try to see the resulting SQL code first. Your project_id may be empty.
Edit
This is a possible solution
Dim projectId As Integer
If Session("project_id") IsNot Nothing AndAlso IsNumeric(Session("project_id")) Then
projectId = Session("project_id")
da = New SqlDataAdapter("select * from revision_cycle_documenttype_mapping where project_id=" & projectId, con)
cb = New SqlCommandBuilder(da)
ds = New DataSet
da.Fill(ds, "revision_cycle_documenttype_mapping") <<--
mapping_count = ds.Tables("revision_cycle_documenttype_mapping").Rows.Count
Response.Write(mapping_count)
End If
I think your session Session("project_id") is empty, and this is not the best way to add a parameter in ur sql statement. your code here is vulnerable to sql injections.
you should first check if your session is empty and then you should add parameters by declaring a sqlcommand and assign parameters to it:
dim project_id as integer = 0
If Session("project_id") IsNot Nothing AndAlso IsNumeric(Session("project_id")) Then
project_id = Session("project_id")
end if
dim cmd as new sqlcommand
cmd.parameters.add("#project_id",project_id)
the rest of your code would stay the same
Use Parameters and check the null.
Dim projectid=0
If Not IsNothing(Session("project_id")) Then
projectid=DirectCast(Session("project_id"),Integer)
End If
Dim sql="select * from revision_cycle_documenttype_mapping where project_id=#project_id"
dim cmd as new SqlCommand(sql,con)
cmd.Parameters.AddWithValue("#project_id",projectid)
da = New SqlDataAdapter=(cmd)
cb = New SqlCommandBuilder(da)
ds = New DataSet
da.Fill(ds, "revision_cycle_documenttype_mapping")
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