Page sorting in ASP.NET - asp.net

I try to add custom paging and when I login through username and password, an error occurs.
Also I show document only his/her documents like when abc is login then abc only able to view her documents, but when I add custom paging it show me error UserID is not supplied and I also share sp code
Code is
protected void BindRepeater()
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["mydms"].
ConnectionString.ToString());
SqlCommand cmd = new SqlCommand("sphrdoc2", con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
PagedDataSource pgitems = new PagedDataSource();
DataView dv = new DataView(dt);
In this line
adp.Fill(dt);
error occurs
Procedure or function 'sphrdoc2' expects parameter '#UserID', which was not supplied.
sp is
ALTER procedure [dbo].[sphrdoc2]
#UserID int
as
Select
dbo.DocumentInfo.DocID as DocumentID,
dbo.DocumentInfo.DocName as DocumentName,
dbo.DocType.DocType as Document,
dbo.Department.DepType as Department,
dbo.DocumentInfo.Uploadfile as FileUploaded,
dbo.ApproveType.ApproveType AS Status
FROM
dbo.DocumentInfo
inner JOIN dbo.DocType ON dbo.DocumentInfo.DocTypeID=dbo.DocType.DocTypeID
inner JOIN dbo.Department ON dbo.DocumentInfo.DepID=dbo.Department.DepID
left join dbo.ApproveType on dbo.DocumentInfo.ApproveID=dbo.ApproveType.ApproveID
where UserID=#UserID
Any one tell me where I done mistake in repeater code?

You need to pass parameters for your stored procedure .
SqlCommand cmd = new SqlCommand("sphrdoc2", con);
cmd.Parameters.AddWithValue("#UserID","YourUserID");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);

Related

I want my table to display all record if no searches were found for a movie but am unsure how to do that

This is my code, tell me where to change because When I do search for a movie that is in a record it displays the result, but when it isn't I only get "Movie Not Found" as in the response.write.
enter image description hereSLNkq.png
I would suggest to put question as text/code instead.
For your case, I suggest you to work with datatable instead of datareader.
== With Datatable approach ==
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("IF EXISTS(SELECT * FROM Content WHERE MovieTitle=#MovieTitle) SELECT * FROM Content WHERE MovieTitle=#MovieTitle ELSE SELECT * FROM Content");
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#MovieTitle", MovieTitle.Text);
cmd.Connection = con;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("Result");
da.Fill(dt);
con.Close();
MovieResults.DataSource = dt;
MovieResults.DataBind();
Based on #Shai Cohen's comment, it would be better approach by using ExecuteDataReader if transaction is forward-only. Thanks #ShaiCohen for your comment.
== Without Datatable approach ==
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("IF EXISTS(SELECT * FROM Content WHERE MovieTitle=#MovieTitle) SELECT * FROM Content WHERE MovieTitle=#MovieTitle ELSE SELECT * FROM Content");
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#MovieTitle", MovieTitle.Text);
cmd.Connection = con;
con.Open();
MovieResults.DataSource = cmd.ExecuteReader();
MovieResults.DataBind();
con.Close();
Enjoy Coding (;

Must declare the scalar variable "#Sid" in gridview while getting single record

while i'm trying to get single record from database table,based on login user details. i'm getting error,let me know where i'm doing wrong.
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
SqlConnection cn = new SqlConnection((cs));
cn.Open();
SqlCommand cmd = new SqlCommand("select * from Student_Details where Sid=#Sid", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.ExecuteNonQuery();
GridView1.DataBind();
Why you are executing query for select statement - cmd.ExecuteNonQuery(); not require..
You have to pass parameter value also
cmd.Parameters.AddWithValue("#sid", sid);
you missed this lines:
cmd.Parameters.AddWithValue("sid", User.Identity.Name)
and also cmd.ExecuteNonQuery(); is not necessary.

I need to loop for all records in my table

I need to loop for all records in my table , cose here is not working properly in my project "Auction web system" , I use web service here to check the status of product Periodically , and when the status is opened and data time is less to now , update the product and set its status to "closed". the code here work only for one row at the time ! I need to check for all rows at the same time.
{ string sql12 = "SELECT item_id FROM items Where status='opened' AND endDate<=#endate ";
SqlCommand cmd12 = new SqlCommand(sql12, con);
con.Open();
cmd12.Parameters.AddWithValue("#endate", DateTime.Now);
query = Convert.ToInt32(cmd12.ExecuteScalar());
string sql123 = "UPDATE items SET status ='closed' WHERE item_id =#Item_ID";
SqlCommand cmd21 = new SqlCommand(sql123, con);
cmd21.Parameters.AddWithValue("#Item_ID", query);
cmd21.ExecuteNonQuery();
con.Close();
CalculateWinningPrice(query);
}
public void CalculateWinningPrice(Int32 query)
{
string sql1 = "SELECT MAX(Bid_price) AS Expr1 FROM Bid WHERE (item_id = #Item_ID)";
SqlCommand cmd1 = new SqlCommand(sql1, con);
con.Open();
cmd1.Parameters.AddWithValue("#Item_ID", query);
Int32 max = Convert.ToInt32(cmd1.ExecuteScalar());
SqlCommand cmd3 = new SqlCommand("SELECT user_id FROM Bid WHERE(Bid_price =(SELECT MAX(Bid_price) AS Expr1 FROM Bid AS BID_1 WHERE(item_id = #Item_ID)))", con);
cmd3.Parameters.AddWithValue("#Item_ID", query);
Int32 winner = Convert.ToInt32(cmd3.ExecuteScalar());
SqlCommand cmd4 = new SqlCommand("SELECT name FROM items WHERE (item_id=#Item_ID)",con);
cmd4.Parameters.AddWithValue("Item_ID", query);
string product_name = Convert.ToString(cmd4.ExecuteScalar());
GeneratePDF.create_pdf(product_name, Convert.ToDecimal(max).ToString("c"), DateTime.Now.ToString());
SqlCommand cmd = new SqlCommand("INSERT INTO Winners VALUES(#item_id, #user_id,#win_price,#win_date)");
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#item_id", query);
cmd.Parameters.AddWithValue("#user_id", winner);
cmd.Parameters.AddWithValue("#win_price", max);
cmd.Parameters.AddWithValue("#win_date", DateTime.Now);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
}
Get results into a datareader MSFT DOCU
then wrap the rest of the code in
while(reader.Read())
{
REST OF CODE
}
This question might also help you.
Reader Question
you can directly update your table by this query
UPDATE items SET status ='closed' WHERE item_id in(SELECT item_id FROM items Where status='opened' AND endDate<=#endate)

I need to loop for all records in table in my database

I need to loop for all records in my table , cose here is not working properly in my project "Auction web system" , I use web service here to check the status of product Periodically , and when the status is opened and data time is less to now , update the
product and set its status to "closed".
the code here work only for one row at the time !
I need to check for all rows at the same time.
{
string sql12 = "SELECT item_id FROM items Where status='opened' AND endDate<=#endate ";
SqlCommand cmd12 = new SqlCommand(sql12, con);
con.Open();
cmd12.Parameters.AddWithValue("#endate", DateTime.Now);
query = Convert.ToInt32(cmd12.ExecuteScalar());
string sql123 = "UPDATE items SET status ='closed' WHERE item_id =#Item_ID";
SqlCommand cmd21 = new SqlCommand(sql123, con);
cmd21.Parameters.AddWithValue("#Item_ID", query);
cmd21.ExecuteNonQuery();
con.Close();
CalculateWinningPrice(query);
}
public void CalculateWinningPrice(Int32 query)
{
string sql1 = "SELECT MAX(Bid_price) AS Expr1 FROM Bid WHERE (item_id = #Item_ID)";
SqlCommand cmd1 = new SqlCommand(sql1, con);
con.Open();
cmd1.Parameters.AddWithValue("#Item_ID", query);
Int32 max = Convert.ToInt32(cmd1.ExecuteScalar());
SqlCommand cmd3 = new SqlCommand("SELECT user_id FROM Bid WHERE(Bid_price =(SELECT MAX(Bid_price) AS Expr1 FROM Bid AS BID_1 WHERE(item_id = #Item_ID)))", con);
cmd3.Parameters.AddWithValue("#Item_ID", query);
Int32 winner = Convert.ToInt32(cmd3.ExecuteScalar());
SqlCommand cmd4 = new SqlCommand("SELECT name FROM items WHERE (item_id=#Item_ID)",con);
cmd4.Parameters.AddWithValue("Item_ID", query);
string product_name = Convert.ToString(cmd4.ExecuteScalar());
GeneratePDF.create_pdf(product_name, Convert.ToDecimal(max).ToString("c"), DateTime.Now.ToString());
SqlCommand cmd = new SqlCommand("INSERT INTO Winners VALUES(#item_id, #user_id,#win_price,#win_date)");
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#item_id", query);
cmd.Parameters.AddWithValue("#user_id", winner);
cmd.Parameters.AddWithValue("#win_price", max);
cmd.Parameters.AddWithValue("#win_date", DateTime.Now);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
}
You can just use one update statement to update all the items status to 'colesed' where the enddate is passed. This is only if you don't need the item_id somewhere else.
So your code above can be;
string sql123 = "UPDATE items SET status ='closed' Where status='opened' AND endDate<=GETDATE()";
SqlCommand cmd21 = new SqlCommand(sql123, con);
cmd21.ExecuteNonQuery();
con.Close();
You can combine both queries into one query and update directly as follows:
Update items
Set status = 'closed'
Where status='opened'
AND endDate<=#endate
Then pass the required value as parameter and run the query with ExecuteNonQuery method.

Detailsview in ASP.NET

I am trying to hit sql server with objectdatasource and return a datatable to fill my details view control. the selected ID value is returned by a gridview control. It seems like the datatable is not filled by adapter, and i couldn't figure out why. The ID in sql is set as a primary key (Int, 4, not null). The debugger says the Detail datatable is null. Any help is much appreciated.
public DataTable GetDetail(string ID)
{
if (ID == "")
{
return null;
}
else
{
DataTable Detail = null;
using (SqlConnection conn = new SqlConnection(connection))
{
string comm = #"select * from dbo.Products where ID = #ID";
conn.Open();
SqlDataAdapter adapter=null;
using (SqlCommand cmd = new SqlCommand(comm, conn))
{
cmd.Parameters.Add("ID", System.Data.SqlDbType.Int, 4).Value = Convert.ToInt32(ID);
adapter = new SqlDataAdapter(cmd);
adapter.Fill(Detail);
return Detail;
}
}
}
I think you missed the commandType
cmd.CommandType = CommandType.Text;
Try this
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(connection))
{
string myquery="select * from dbo.Products where ID = #ID";
SqlCommand cmd = new SqlCommand(myquery, con);
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
cmd.Parameters.Add("#ID", SqlDbType.NVarChar, 15).Value = ID;
dap.Fill(ds);
return ds.Tables[0];
}
Thanks for ALL.
The problem is I didnt initialize my datatable to a new instance.
DataTable Detail = null; ===> DataTable Detail = new Datatable();
and also the convert should be done in sql not in codes.
cmd.Parameters.Add("ID", System.Data.SqlDbType.Int, 4).Value = ID;
string comm = #"select * from dbo.Products where ID = convert(int,#ID)";

Resources