I am quite new to ASP.NET and I am making my first application for my school project and I have one problem.
I have my database for adding files and pictures. I used a SqlDataSource and DataList, so I generate insert, update and delete automatically. In code-behind C#, I wrote code for adding images directly to database and file with path that goes to database, but I don't know how to add value (path) to parameter so it could all be saved together. What command should I use?
Maybe it is stupid question, but I don't have anyone to ask. Sorry If my code is a little bit funny looking here in this windowenter code here
Here is my C#
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
FileUpload kontrola = (FileUpload)FormView1.FindControl("FileUpload2");
if (kontrola.PostedFile != null && kontrola.PostedFile.FileName != "")
{
BinaryReader reader = new BinaryReader(kontrola.PostedFile.InputStream);
myImage = reader.ReadBytes(kontrola.PostedFile.ContentLength);
}
System.Data.SqlClient.SqlParameter uploadData =
new System.Data.SqlClient.SqlParameter("#image",System.Data.SqlDbType.Image);
uploadData.Value = myImage;
e.Command.Parameters.Add(uploadData);
FileUpload kontrola2 = (FileUpload)FormView1.FindControl("FileUpload1");
//Get Filename from fileupload control
string filename = Path.GetFileName(kontrola2.PostedFile.FileName);
//Save images into Images folder
kontrola2.SaveAs(Server.MapPath("~/files/" + kontrola2.FileName));
string valp = "files/" + kontrola2.FileName; // I am not sure if this is right
//e.Command.Parameters.Add["#data_file"].Value = valp; // this is not right, but this part I don't know
}
and this is my markup in aspx.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:repository_dbConnectionString %>"
SelectCommand="SELECT * FROM [data]"
DeleteCommand="DELETE FROM [data] WHERE [id] = #id"
InsertCommand="INSERT INTO [data] ([data_set_name], [abstract], [source], [multivariate], [unvariate], [sequentional], [time_series], [text], [domain_theory], [clasification], [regression], [clustering], [casual_discovery], [categorical], [integer], [real], [life_sciences], [physical_sciences], [cs_engineering], [social_sciences], [business], [game], [other], [matrix], [non_matrix], [yes], [no], [num_istances], [num_attributes], [relevant_info], [attribute_info], [relevant_papers], [citation_request], [data_file], [image], [date], [approval]) VALUES (#data_set_name, #abstract, #source, #multivariate, #unvariate, #sequentional, #time_series, #text, #domain_theory, #clasification, #regression, #clustering, #casual_discovery, #categorical, #integer, #real, #life_sciences, #physical_sciences, #cs_engineering, #social_sciences, #business, #game, #other, #matrix, #non_matrix, #yes, #no, #num_istances, #num_attributes, #relevant_info, #attribute_info, #relevant_papers, #citation_request, #data_file, #image, GetDate(), #approval)"
UpdateCommand...
oninserting="SqlDataSource1_Inserting">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="data_set_name" Type="String" />
<asp:Parameter Name="abstract" Type="String" />
<asp:Parameter Name="source" Type="String" />
<asp:Parameter Name="multivariate" Type="Boolean" />
<asp:Parameter Name="unvariate" Type="Boolean" />
<asp:Parameter Name="sequentional" Type="Boolean" />
<asp:Parameter Name="time_series" Type="Boolean" />
<asp:Parameter Name="text" Type="Boolean" />
<asp:Parameter Name="domain_theory" Type="Boolean" />
<asp:Parameter Name="clasification" Type="Boolean" />
<asp:Parameter Name="regression" Type="Boolean" />
<asp:Parameter Name="clustering" Type="Boolean" />
<asp:Parameter Name="casual_discovery" Type="Boolean" />
<asp:Parameter Name="categorical" Type="Boolean" />
<asp:Parameter Name="integer" Type="Boolean" />
<asp:Parameter Name="real" Type="Boolean" />
<asp:Parameter Name="life_sciences" Type="Boolean" />
<asp:Parameter Name="physical_sciences" Type="Boolean" />
<asp:Parameter Name="cs_engineering" Type="Boolean" />
<asp:Parameter Name="social_sciences" Type="Boolean" />
<asp:Parameter Name="business" Type="Boolean" />
<asp:Parameter Name="game" Type="Boolean" />
<asp:Parameter Name="other" Type="Boolean" />
<asp:Parameter Name="matrix" Type="Boolean" />
<asp:Parameter Name="non_matrix" Type="Boolean" />
<asp:Parameter Name="yes" Type="Boolean" />
<asp:Parameter Name="no" Type="Boolean" />
<asp:Parameter Name="num_istances" Type="Int32" />
<asp:Parameter Name="num_attributes" Type="Int32" />
<asp:Parameter Name="relevant_info" Type="String" />
<asp:Parameter Name="attribute_info" Type="String" />
<asp:Parameter Name="relevant_papers" Type="String" />
<asp:Parameter Name="citation_request" Type="String" />
<asp:Parameter Name="data_file" Type="String" />
<asp:Parameter DbType="Date" Name="date" />
<asp:Parameter Name="approval" Type="Boolean" />
</InsertParameters>
You've specified the Insert parameters in the SqlDataSource, so they'll already exist in the Parameters collection. You only need to call:
e.Command.Parameters["#data_file"].Value = ....
you can use in front-End aspx put edit text and FileUpload
after that in the back-End C# use same this
var conn = ConfigurationManager.ConnectionStrings["YesserDB"].ConnectionString;
using (var connection = new SqlConnection(conn))
{
SqlCommand cmd = new SqlCommand("INSERT INTO testOne (DocNo,DocSysNo,DocTypeID,DocDate,DocDateHj,DocSubjectID,DocDestinaionID,DocSoureceID,DocConfidentionyID,DocPriorityID,DocRemarks,DocAttributes) VALUES (#DocNo,#DocSysNo,#DocTypeID,#DocDate,#DocDateHj,#DocSubjectID,#DocDestinaionID,#DocSoureceID,#DocConfidentionyID,#DocPriorityID,#DocRemarks,#DocAttributes)", connection);
cmd.Parameters.AddWithValue("#DocNo", DocNo );
cmd.Parameters.AddWithValue("#DocSysNo", DocSysNo );
cmd.Parameters.AddWithValue("#DocTypeID", DocTypeID );
cmd.Parameters.AddWithValue("#DocDate", DocDate);
cmd.Parameters.AddWithValue("#DocDateHj", DocDateHj);
cmd.Parameters.AddWithValue("#DocSubjectID", DocSubjectID );
cmd.Parameters.AddWithValue("#DocDestinaionID",DocDestinaionID );
cmd.Parameters.AddWithValue("#DocSoureceID",DocSoureceID );
cmd.Parameters.AddWithValue("#DocConfidentionyID", DocConfidentionyID);
cmd.Parameters.AddWithValue("#DocPriorityID", DocPriorityID);
cmd.Parameters.AddWithValue("#DocRemarks", DocRemarks);
cmd.Parameters.AddWithValue("#DocAttributes", DocAttributes);
//cmd.Parameters.AddWithValue("#AttachFile", FileUpload1.TemplateControl);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Hello to all respected members,
Presently I using following SELECT command in asp.net for populating a Gridview based on Query string values:
SelectCommand="SELECT ID, Date, Train, I_R, Dir_Ind, Detn, Rly, DiV, Loco, Shed, locoClass, loco_type FROM PunctualityMain WHERE Date >= #strdt AND Date <= #enddt AND ([Rly] = #rly OR #rly IS NULL) AND ([shed] = #shed OR #shed IS NULL) Order by Date ASC">
<SelectParameters>
<asp:QueryStringParameter Name="strdt" QueryStringField="strdt" Type="DateTime" />
<asp:QueryStringParameter Name="enddt" QueryStringField="enddt" Type="DateTime" />
<asp:QueryStringParameter Name="rly" QueryStringField="rly" Type="string" />
<asp:QueryStringParameter Name="shed" QueryStringField="shed" Type="string" />
</SelectParameters>
It is working fine also with two values null as :
http://elocos.railnet.gov.in/Punctuality/Punctualitydetail.aspx?strdt=5/1/2013&enddt=5/14/2013&rly=&shed=
Now I want to use same for Gridview with Delete, Insert & Update Commands which is as under:
DeleteCommand="DELETE FROM [PunctualityMain] WHERE [ID] = #ID"
InsertCommand="INSERT INTO [PunctualityMain] ([Date], [Train], [Dir_Ind], [Detn], [Rly], [DiV], [Loco], [Shed], [locoClass], [loco_type]) VALUES (#Date, #Train, #Dir_Ind, #Detn, #Rly, #DiV, #Loco, #Shed, #locoClass, #loco_type)"
SelectCommand="SELECT * FROM [PunctualityMain] WHERE (([Date] >= #strdt) AND ([Date] <= #enddt) AND ([Shed] = #shed OR #shed IS NULL) AND ([Rly] = #rly OR #rly IS NULL)) ORDER BY [Date]"
UpdateCommand="UPDATE [PunctualityMain] SET [Date] = #Date, [Train] = #Train, [Dir_Ind] = #Dir_Ind, [Detn] = #Detn, [Rly] = #Rly, [DiV] = #DiV, [Loco] = #Loco, [Shed] = #Shed, [locoClass] = #locoClass, [loco_type] = #loco_type WHERE [ID] = #ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Train" Type="Int32" />
<asp:Parameter Name="Dir_Ind" Type="String" />
<asp:Parameter Name="Detn" Type="Int32" />
<asp:Parameter Name="Rly" Type="String" />
<asp:Parameter Name="DiV" Type="String" />
<asp:Parameter Name="Loco" Type="String" />
<asp:Parameter Name="Shed" Type="String" />
<asp:Parameter Name="locoClass" Type="String" />
<asp:Parameter Name="loco_type" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="strdt" QueryStringField="strdt" Type="DateTime" />
<asp:QueryStringParameter Name="enddt" QueryStringField="enddt" Type="DateTime" />
<asp:QueryStringParameter Name="rly" QueryStringField="rly" Type="string" />
<asp:QueryStringParameter Name="shed" QueryStringField="shed" Type="string" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Train" Type="Int32" />
<asp:Parameter Name="Dir_Ind" Type="String" />
<asp:Parameter Name="Detn" Type="Int32" />
<asp:Parameter Name="Rly" Type="String" />
<asp:Parameter Name="DiV" Type="String" />
<asp:Parameter Name="Loco" Type="String" />
<asp:Parameter Name="Shed" Type="String" />
<asp:Parameter Name="locoClass" Type="String" />
<asp:Parameter Name="loco_type" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
Why the above SELECT command not working with NULL values for rly & shed? However, with all the four query string values it is working like this
http://elocos.railnet.gov.in/Punctuality/Punctualitydetail.aspx?strdt=5/1/2013&enddt=5/14/2013&rly=CR&shed=KYN
Please suggest any solution for the problem so that it can work with this string also (NULL string values)
http://elocos.railnet.gov.in/Punctuality/Punctualitydetail.aspx?strdt=5/1/2013&enddt=5/14/2013&rly=&shed=
Because you get Empty value from empty querystring value =/= null
In order to get this working you should set null value manually.
I can suggest you at least 2 ways:
1) In codebehind:
string rly = Request.QueryString["rly"];
if (rly == "")
rly = null;
And then use it.
2) In SQL command:
Rly=NullIf('#rly','')
But I'm not sure if 2nd one will work like this, if it won't then you can try to workaround it like:
codebehind string empt = "";
SQL command Rly=NullIf('#rly','+empt+')
Or you can go some other way, the point is to verify it "rly" query is "", if it is then set it to null, otherwise leave it alone.
I have the following SqlDataSource on a GridView. All works except for Delete Function.
What am I doing wrong , no error is given but no result either.
How can this be solved?
<asp:SqlDataSource ID="BookingsSQL" runat="server" ConnectionString="<%$ ConnectionStrings:BookingsConnectionString %>"
OldValuesParameterFormatString="original_{0}" ConflictDetection="OverwriteChanges"
SelectCommand="SELECT [ID], [programme name] AS programme_name, [Start], [Finish], [Source], [Destination], [Comment] FROM [Bookings]"
DeleteCommand="DELETE FROM [Bookings] WHERE [programme name] = #ID" InsertCommand="INSERT INTO [Bookings] ([programme name], [Start], [Finish], [Source], [Destination], [Comment]) VALUES (#programme_name, #Start, #Finish, #Source, #Destination, #Comment)"
UpdateCommand="UPDATE [Bookings] SET [programme name] = #programme_name, [Start] = #Start, [Finish] = #Finish, [Source] = #Source, [Destination] = #Destination, [Comment] = #Comment WHERE [ID] = #ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="programme_name" Type="String" />
<asp:Parameter Name="Start" Type="DateTime" />
<asp:Parameter Name="Finish" Type="DateTime" />
<asp:Parameter Name="Source" Type="String" />
<asp:Parameter Name="Destination" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="Start" Type="DateTime" />
<asp:Parameter Name="Finish" Type="DateTime" />
<asp:Parameter Name="Source" Type="String" />
<asp:Parameter Name="Destination" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
<asp:Parameter Name="programme_name" Type="String" />
</UpdateParameters>
I think this:
DELETE FROM [Bookings] WHERE [programme name] = #ID
should be this:
DELETE FROM [Bookings] WHERE [ID] = #ID
You delete by [programme name] where from your other queries it looks like you want to delete by the [ID] column.
Since that columns exists, you won't get an error but also no matches to delete. Change the query to:
DELETE FROM Bookings WHERE ID=#ID
<asp:AccessDataSource ID="productsubtype" Runat="server" DataFile="~/access_db/db_tables.mdb"
SelectCommand="SELECT [type], [id],[subtype] FROM [prod_subtype]"
UpdateCommand="UPDATE [prod_subtype] SET [subtype]=#subtype WHERE [id]=#id"
DeleteCommand="DELETE FROM [prod_subtype] WHERE [id]=#id"
FilterExpression="type='{0}'">
<UpdateParameters>
<asp:Parameter Name="subtype" Type="string" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="string" />
</DeleteParameters>
<FilterParameters>
<asp:ControlParameter Name="type" DefaultValue="-1"
Type="String" ControlID="gdproducttype"
PropertyName="SelectedValue"></asp:ControlParameter>
</FilterParameters>
</asp:AccessDataSource>
When i press delete button in gridview i am getting error
Try changing your UpdateParameters to the following:
<UpdateParameters>
<asp:Parameter Name="id" Type="string" />
<asp:Parameter Name="subtype" Type="string" />
</UpdateParameters>
I would like to insert UserName from aspnet_users into my own table called Members. My sqldatasource looks like as follows. At the end in the InsertParameters I have added ProfileParameter but its not working. Just to get the values going, I have hardcoded 'testhere' value in the insertcommand. My SQLDatasource looks as follows.
Please help !!!
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
OnInserted= "on_inserted"
OnUpdated = "on_updated"
ConnectionString="<%$ ConnectionStrings:test1ConnectionString %>"
DeleteCommand="DELETE FROM [member] WHERE [id] = #id"
InsertCommand="INSERT INTO [member] ([firstname], [lastname],[username]) VALUES (#firstname, #lastname,'testhere');
set #id = ##identity"
SelectCommand="SELECT * FROM [member] WHERE ([id] = #id)"
UpdateCommand="UPDATE [member] SET [firstname] = #firstname, [lastname] = #lastname WHERE [id] = #id"
onselecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="firstname" Type="String" />
<asp:Parameter Name="lastname" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="id" Type=Int32 Direction=Output />
<asp:Parameter Name="firstname" Type="String" />
<asp:Parameter Name="lastname" Type="String" />
<asp:ProfileParameter Name="username" Type=String />
</InsertParameters>
</asp:SqlDataSource>
Okay.. as a simple one:
Try changing the InsertParameter (username) to be an asp:Parameter.
Add a asp:HiddenField control with ID of username
onpageload, set the value of the username hiddenfield to be Page.User.Identity.Name
Magic!