You have a listbox where you need to deal with multiple items selected. I guess this should be very easy, but still it's worth to write about it.
Parameter p;
for (int i = 0; i < listbox.Items.Count; i++)
{
if (listbox.Items[i].Selected)
{
p = new Parameter("param", TypeCode.String, listbox.Items[i].Value);
myDS.InsertParameters.Add(p);
myDS.Insert();
myDS.InsertParameters.Remove(p);
}
}
Saturday
Command button in a Gridview
Suppose you are displaying records in a gridview and would like to create a command button where you click on and send an email to the memberID for example.
In case you are not displaying the memberID, you should use the following syntax for doing the job:
if (e.CommandName.ToString() == "cmdContact")
{
int MemberID = (int)GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
//then do something with the memberID value.
In case you are not displaying the memberID, you should use the following syntax for doing the job:
if (e.CommandName.ToString() == "cmdContact")
{
int MemberID = (int)GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
//then do something with the memberID value.
Thursday
identity insert with SQL (instead of SQL procedure)
You can do the same start you did with the stored procediure. Except that in the _Inserted Event you do:
DbCommand command = e.Command;
string eid;
command.CommandText = "SELECT @@IDENTITY";
eid= command.ExecuteScalar().ToString();
DbCommand command = e.Command;
string eid;
command.CommandText = "SELECT @@IDENTITY";
eid= command.ExecuteScalar().ToString();
How to save a word document in the database.
How to save a word document in the database.
protected void btnFileUpload_Click()
{
byte[] fileData = null;
try
{
string FileExtention;
if ((m_File != null)&&(m_File.PostedFile != null) && (m_File.PostedFile.ContentLength > 0))
{
int size = m_File.PostedFile.ContentLength;
if (size < 102400)
{
if (ExtentionOK("pdf") || ExtentionOK("doc") || ExtentionOK("rtf") || ExtentionOK("txt"))
{
FileExtention = System.IO.Path.GetExtension(m_File.PostedFile.FileName);
string fn = System.IO.Path.GetFileName(m_File.PostedFile.FileName);
Stream MyStream = m_File.PostedFile.InputStream;
long iLength = MyStream.Length;
fileData = new byte[(int)MyStream.Length];
MyStream.Read(fileData, 0, (int)MyStream.Length);
MyStream.Close();
SqlConnection MyConnection = new SqlConnection("Data Source=myserver;Integrated Security=SSPI;Initial Catalog=Mydatabase");
MyConnection.Open();
SqlCommand MyCommand = new SqlCommand("INSERT INTO mytable (myfile) VALUES (@doc_content)", MyConnection);
SqlParameter param1 = new SqlParameter("@doc_content", SqlDbType.Image);
param1.Value = fileData;
MyCommand.Parameters.Add(param1);
MyCommand.ExecuteNonQuery();
}
else
Reponse.Write("File extention not allowed");
}
else
Reponse.Write("File too big. You are only allowed 100K for the resume");
}
else
Reponse.Write("Please select a file to upload");
}
catch (Exception excep)
{
ExceptionCatching(excep);
}
protected void btnFileUpload_Click()
{
byte[] fileData = null;
try
{
string FileExtention;
if ((m_File != null)&&(m_File.PostedFile != null) && (m_File.PostedFile.ContentLength > 0))
{
int size = m_File.PostedFile.ContentLength;
if (size < 102400)
{
if (ExtentionOK("pdf") || ExtentionOK("doc") || ExtentionOK("rtf") || ExtentionOK("txt"))
{
FileExtention = System.IO.Path.GetExtension(m_File.PostedFile.FileName);
string fn = System.IO.Path.GetFileName(m_File.PostedFile.FileName);
Stream MyStream = m_File.PostedFile.InputStream;
long iLength = MyStream.Length;
fileData = new byte[(int)MyStream.Length];
MyStream.Read(fileData, 0, (int)MyStream.Length);
MyStream.Close();
SqlConnection MyConnection = new SqlConnection("Data Source=myserver;Integrated Security=SSPI;Initial Catalog=Mydatabase");
MyConnection.Open();
SqlCommand MyCommand = new SqlCommand("INSERT INTO mytable (myfile) VALUES (@doc_content)", MyConnection);
SqlParameter param1 = new SqlParameter("@doc_content", SqlDbType.Image);
param1.Value = fileData;
MyCommand.Parameters.Add(param1);
MyCommand.ExecuteNonQuery();
}
else
Reponse.Write("File extention not allowed");
}
else
Reponse.Write("File too big. You are only allowed 100K for the resume");
}
else
Reponse.Write("Please select a file to upload");
}
catch (Exception excep)
{
ExceptionCatching(excep);
}
How to get a return value from a stored procedure using .net 2.0
How to get a return value from a stored procedure using .net 2.0
Suppose you are working with a stored procedure that insert a specific record that has identity on. You want then to return that identity number to insert in another table:
Here is the stored procedure code:
CREATE PROCEDURE dbo.InsertEvent_sp(@EventID INT =0 ,--this is the identity column@EventDate smalldatetime ,
@EventTitle nvarchar(100))AS
BEGIN SET NOCOUNT ON INSERT
INTO tblEvent (EventDate, EventTitle)
values (@EventDate, @EventTitle)SET @EventID = @@IDENTITY
return @EventID
END
GO
Then you would need to use this stored procedure in your SqlDataSource...Then once you insert the record using the datasource, you have to creat a new event for the inserted event: OnInserted="EventDS_Inserted" in the SqlDataSource tag.
Then in the code: EventDS_Inserted would be like:
protected void EventDS_Inserted(object sender, SqlDataSourceStatusEventArgs e){
DbCommand command = e.Command; //do not forget to add using System.Data.Common; in the declarations section.
string eid;
string Speaker, SpeakerTitle;
eid = command.Parameters["@EventID"].Value.ToString();
}
Suppose you are working with a stored procedure that insert a specific record that has identity on. You want then to return that identity number to insert in another table:
Here is the stored procedure code:
CREATE PROCEDURE dbo.InsertEvent_sp(@EventID INT =0 ,--this is the identity column@EventDate smalldatetime ,
@EventTitle nvarchar(100))AS
BEGIN SET NOCOUNT ON INSERT
INTO tblEvent (EventDate, EventTitle)
values (@EventDate, @EventTitle)SET @EventID = @@IDENTITY
return @EventID
END
GO
Then you would need to use this stored procedure in your SqlDataSource...Then once you insert the record using the datasource, you have to creat a new event for the inserted event: OnInserted="EventDS_Inserted" in the SqlDataSource tag.
Then in the code: EventDS_Inserted would be like:
protected void EventDS_Inserted(object sender, SqlDataSourceStatusEventArgs e){
DbCommand command = e.Command; //do not forget to add using System.Data.Common; in the declarations section.
string eid;
string Speaker, SpeakerTitle;
eid = command.Parameters["@EventID"].Value.ToString();
}
Labels:
.Net,
Insert,
SqlDataSource,
Stored Procedures,
Visual Studio 2005,
Web2.0
How to access Formview textfield Data
How to access Formview textfield Data
If you have a formview, and want to access its controls, here is the code for it:
((HiddenField) MyFormView.FindControl("EventDate")).Value= SomeValue;
Of course, you can replace the (HiddenField) with something else, like (TextBox), (CheckBox), etc.
If you have a formview, and want to access its controls, here is the code for it:
((HiddenField) MyFormView.FindControl("EventDate")).Value= SomeValue;
Of course, you can replace the (HiddenField) with something else, like (TextBox), (CheckBox), etc.
How to access data read by a SqlDataSource
How to access data read by a SqlDataSource
You have a datasource that returs data. And you want to access a specific field in it:
DataView dv = (DataView)MyDS.Select(DataSourceSelectArguments.Empty);DataRowView drv = dv[0];// or dr and loop throu the dvs... dv[0] assuming you only have one row.
then Variable = drv["myfield].ToString();
Good luck
You have a datasource that returs data. And you want to access a specific field in it:
DataView dv = (DataView)MyDS.Select(DataSourceSelectArguments.Empty);DataRowView drv = dv[0];// or dr and loop throu the dvs... dv[0] assuming you only have one row.
then Variable = drv["myfield].ToString();
Good luck
Subscribe to:
Posts (Atom)