Category Archives: Table

[RESOLVED] listbox with optgroup

is there way to do listbox with optgroup?
i tried just making a header row with different styles but it is still selectable. enabled for that row makes it invisible.
i dont know of a way to use just a plain <select with runat=server and populate the items from code behind.

I did find someones dll that has this ability in their user control but i get a security trust fail when trying to run it. I cannot figure out how to tell the web.config that the dll is safe to run.

 

Doug 

http://weblogs.asp.net/jeff/archive/2006/12/27/dropdownlist-with-optgroup.aspx

 

Check Comment of -  Ben Sloan

The answer is not to use attributes as you suggest, but instead to set the "value" of the listitem which is to be the group header. Set it to something very unlikely to be used again like "$$GROUPHEADER$$GROUPHEADER$$"

Your test against the attribute (to see whether it is null) then becomes: "If item.Value = "$$GROUPHEADER$$GROUPHEADER$$" Then"

Next is to make it a bit easier for the programmer to add groups without having to remember this value string. To do this, I created a class called "OptionGroup" (I'm sure you can work out the contents of it) and then added a public sub on the control you wrote:

       Public Sub AddOptionGroup(ByVal og As OptionGroup)

           Dim optGrpTitle As New ListItem(og.GroupName, "$$GROUPHEADER$$GROUPHEADER$$")

           Me.Items.Add(optGrpTitle)

           For Each li As ListItem In og.Items

               Me.Items.Add(New ListItem(li.Text, li.Value))

           Next

       End Sub

To then add groups in an .aspx page:

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       If Not IsPostBack Then

           Dim G1Items As New ListItemCollection

           G1Items.Add(New ListItem("G1V1", "G1V1"))

           G1Items.Add(New ListItem("G1V2", "G1V2"))

           G1Items.Add(New ListItem("G1V3", "G1V3"))

           Dim G2Items As New ListItemCollection

           G2Items.Add(New ListItem("G2V1", "G2V1"))

           G2Items.Add(New ListItem("G2V2", "G2V2"))

           G2Items.Add(New ListItem("G2V3", "G2V3"))

           Dim og1 As New OptionGroup("Group1", G1Items)

           Dim og2 As New OptionGroup("Group2", G2Items)

           Me.ddlTest.AddOptionGroup(og1)

           Me.ddlTest.AddOptionGroup(og2)

       End If

   End Sub

Except for that the code remains the same, and works perfectly across postbacks.

This is because the value property of the items does persist in viewstate, I suppose.

thx for that suggestion. I finally found out my problems with adding in someones dll. I had to change the app pool to run under NetworkService :) 

If you want to work w/ optgroup -- you need to use CustomValidator for Validation

Request.Form to get that value on server end

And throw JS from codebehind to update that control

Something along this line

<select name="opt1" id="opt1" >
        <option selected="selected" value="1">Select One</option>
        <optgroup label="aaaa">
        <option value="2">z1</option>
        <option value="3">z2</option>
        <option value="4">Z3</option></optgroup>
        <optgroup label="bbbb">
        <option value="5">X1</option>
        <option value="6">X2</option>
        </optgroup>
       
    </select>


//To get value at server end
int y1  = Int32.Parse(Request.Form["opt1"].ToString());


//To update that control from codebehind
ScriptManager.RegisterStartupScript(this, this.GetType(), "starScript", "Updateopt('" + x1 + "');", true);

[RESOLVED] 1 of 2 inserts isnt working but button event is triggered

i have 2 inserts that i need to occur on a button, its nothing fancy, just capturing 5 values into the table. I have placed break points in the button event and its being triggered and all values are being set and passed into the stored procedure call. But one of the 2 isnt working... i can execute that procedure on the database side and works fine.. im not getting any errors when submitting the form but 1 of the 2 is not being triggered...below is my button event, the scheduleddownloadInsert is the one that isnt happening, when i debug both inserts are getting the values expected and when i step into the procedure call in my class, the values are present there as well, so everything is getting the values and making it to the database call, but only the audit insert works.. i compared the stored procedures and everything is setup the same and both work fine when manually executed on the SQL side.

 

protected void btSchedule_Click(object sender, EventArgs e)
        {
            string stTime = ddlSHH.SelectedValue + ":" + ddlSMM.SelectedValue + ":" + ddlSSS.SelectedValue;
            string enTime = ddlEHH.SelectedValue + ":" + ddlEMM.SelectedValue + ":" + ddlESS.SelectedValue;


            //USED TO CAPTURE SCHEDULED DOWNLOAD FOR LATER PROCESSING
            dac.ScheduledDownloadInsert(ddlDVR.SelectedValue, txtSdate.Text, stTime, txtEdate.Text, enTime, "0", dac.usn);


            //USED TO CAPTURE SCHEDULED DOWNLOAD IN AUDIT TABLE
            dac.insertauditintosql(dac.usn, "Scheduled Download", ddlDVR.SelectedValue + " " + txtSdate.Text + " " + stTime + " " + txtEdate.Text + " " + enTime);
        }


 

please share the codes of two methods used to insert data ScheduledDownloadInsert and the auditinsert. there should be any problem with ScheduledDownloadInsert.

public void ScheduledDownloadInsert(string nodeid, string sdate, string stime, string edate, string etime, string processed, string scheduledby)
    {
        using (SqlConnection conn = new SqlConnection(this.connDB))
        using (SqlCommand cmd = new SqlCommand())
            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = "ScheduledDownloadsInsert";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Clear();


                cmd.Parameters.Add("@nodeid", System.Data.SqlDbType.Int).Value = nodeid;
                cmd.Parameters.Add("@sdate", System.Data.SqlDbType.VarChar).Value = sdate;
                cmd.Parameters.Add("@stime", System.Data.SqlDbType.VarChar).Value = stime;
                cmd.Parameters.Add("@edate", System.Data.SqlDbType.VarChar).Value = edate;
                cmd.Parameters.Add("@etime", System.Data.SqlDbType.VarChar).Value = etime;
                cmd.Parameters.Add("@processed", System.Data.SqlDbType.Bit).Value = processed;
                cmd.Parameters.Add("@scheduledby", System.Data.SqlDbType.VarChar).Value = scheduledby;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Scheduled Download Insert Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                conn.Close();
            }
    }




    public void insertauditintosql(string UserName, string action, string otherdata)
    {
        using (SqlConnection conn = new SqlConnection(this.connDB))
        using (SqlCommand cmd = new SqlCommand())
            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = "AuditiNSERT";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Clear();


                cmd.Parameters.Add("@UserName", System.Data.SqlDbType.VarChar).Value = UserName;
                cmd.Parameters.Add("@action", System.Data.SqlDbType.VarChar).Value = action;
                cmd.Parameters.Add("@otherdata", System.Data.SqlDbType.VarChar).Value = otherdata;


                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Audit Insert Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                conn.Close();
            }
    }


 

You are missing the execute statement in "ScheduledDownloadInsert"  :) :)

cmd.ExecuteNonQuery();

i hate when its something simple, i dont know how many times i compared the two inserts and others and didnt even notice tht was missing..

 

thanks.. that took care of it

 

[RESOLVED] Database value save

<div style="margin-top: 10.08pt; margin-bottom: 0pt; margin-left: 0.38in; text-indent: -0.38in; text-align: left; direction: ltr; unicode-bidi: embed; vertical-align: baseline;">enrolls (sid, coursecodesemester, year)</div><div style="margin-top: 10.08pt; margin-bottom: 0pt; margin-left: 0.38in; text-indent: -0.38in; text-align: left; direction: ltr; unicode-bidi: embed; vertical-align: baseline;">suppose Student enroll 5 or 6 courses(coursecode) of semester 1. how i can save in my table like this
</div>

sid

Course code

Semester

Year

b1

Csc100

1

2010

b1

Csc101

1

2010

b1

Csc102

1

2010

b1

Csc103

1

2010

b1

Csc104

1

2010

1)sid=sessionid

2)Dropdownlist select semester ==>> selected semester courses in a listbox

3)Year select


insert into enroll(sid,Semester,coursecode,year) values (@sid,@semester,@coursecode,@year) .

<div style="margin-top: 10.08pt; margin-bottom: 0pt; margin-left: 0.38in; text-indent: -0.38in; text-align: left; direction: ltr; unicode-bidi: embed; vertical-align: baseline;">
</div><div style="margin-top: 10.08pt; margin-bottom: 0pt; margin-left: 0.38in; text-indent: -0.38in; text-align: left; direction: ltr; unicode-bidi: embed; vertical-align: baseline;">
</div><div style="margin-top: 10.08pt; margin-bottom: 0pt; margin-left: 0.38in; text-indent: -0.38in; text-align: left; direction: ltr; unicode-bidi: embed; vertical-align: baseline;">
</div>

Hi,

This table is not enough.It is Bad Design.see how you will design tabes for it.

This gif is from 

Entity-Relationship (ER) Diagrams



Next You can Create View and Insert in that view otherwise you can use

Insert statement for Multiple tables.


Regards


I have to save Listbox  values in to my table (listbox multiply values i.e 6 vales)

listbox=coursecode


foreach (ListItem listitem in ListBox1.Items)

selected vales save table



Hi,

Ok if you don't bother about database design It is big prob from tomm.Anyways just see how you can write sql query to insert multiple records in table


Tutorial 3 - Adding Multiple Records to a Child Table Using Stored ...

[Solved] How to Insert multiple values from a listbox or ...

DotNet Asp: Inserting multiple values from a listbox into a ...


You can See this thread where someone has already resolved this issue

Insert Multiple Selected Items of a List box into table - ASP.NET ...

Regards

shabir

[RESOLVED] Import data from text and excel

Hello, I am working on a C# web application under visual studio 2005.

I want to import data from text file or excel file into a SQL 2005 database.
I have 3 columns in the text file separated by |, example of the text file format:

1|11122222|Name1
2|22299809|Name2

Can you tell me how to import data from text or excel file?

Another question, if you provide a solution for the import from excel, can you note which office (2003 or 2007 or 2010)?

Thank you.

lebdeveloper

Another question, if you provide a solution for the import from excel, can you note which office (2003 or 2007 or 2010)?
 

Check this solution..

http://weblogs.asp.net/rajbk/archive/2009/05/02/uploading-an-excel-file-to-sql-through-an-asp-net-webform.aspx

lebdeveloper

I have 3 columns in the text file separated by |, example of the text file format:

Break each line into string array with char separator "|". After that you can use above code to upload.

Hi, firt of all, you need to know the path of the file. I recommend you use cvs files for working with excel.

Any Excel can open this kind of file. Then You should do something like this:

StreamReader sw = new StreamReader("path of file + file + .extension");


while(!sw.EndofStream)
{
string[] partsOfString = sw.ReadLine().split('|');


set a parameter = partsOfString[0];
set a parameter = partsOfString[1];
set a parameter = partsOfString[2];


command.parameters.add(params);
command.ExecuteInsert();
}


All this doing normal process to insert into a database right?


lebdeveloper

Can you tell me how to import data from text or excel file?

You can use OLeDB connection for reading a Xls file...and you can use StreamReader to read the txt file....

When you read xls file, you can fill the dataset using that data and then call a stored proc in Sql DB to make bulk insert....the easient way to do is, convert the dataset to Xml, and give that xml as input to the stored proc...and in the stored proc you can easily parse the xml and make inserts into the DB....

For txt file, you got to parse the string which you get out of streamreader, and then write some code to convert it into DataTable which will be merged to a dataset, then follow the above procedure....

PS - I have given you conceptual idea on how it works...you got read with some articles on line on how to accomplish the above said tasks...may be you can break them into smaller tasks and go one at a time...

Thanks.

[RESOLVED] url routing with asp.net 3.5 or 4

Hi I need to fix url routing different from the basics that is builtin in the framework...Basically I need it to go unlimited levels deep the same as the product catalog can when using the parent relation for each category added.

does anyone have a good sample for webforms of how I could do it...I want URL listings to look like this

www.mydomain.com/category/category2/category3/ or like this www.mydomain.com/category/category2/category3.aspx

and details URL looking like this

www.mydomain.com/category/category2/category3/title-of-article-or-productname.aspx

 

Thanks,

 

I don't know about version 3.5 but in 4.0 this should be okay.

I would just use the standard ASP.NET 4.0 routing and implement them like this:

routes.MapPageRoute("", "/Category/{cat2}", "~/Category.aspx");

<div>

routes.MapPageRoute("", "/Category/{cat2}/{cat3}", "~/Category.aspx");

</div><div>

routes.MapPageRoute("", "/Category/{cat2}/{cat3}/{cat4}", "~/Category.aspx");

The depth is not unlimited but I'm guessing there will be a limit to the number of levels you'll need to go.

</div><div>
</div>

This tutorial might help you....

http://msdn.microsoft.com/en-us/library/cc668201.aspx

Thanks.

this might help you  http://msdn.microsoft.com/en-us/magazine/dd347546.aspx#id0070003

thanks for your answers guys....these examples covers the basics...which is quite easy to implement. Im looking for something that can handle more complex structures and preferably can come up automatically as categories are added to the database without any manual tampering in global.asax

Basically I want to know if it is possible to dynamically generate the rounting table without registering them in Global.asax.

If not I will have to register 24 routes for the product catalogue (6 base routes with limit to 3 subcategories deep) itself and then a few for other kind of pages so a total of 26 routes...and it will have to be maintained everytime a new base category is registered then I have to register 4 new routes...it wont happend often but does anyone have any ideas...?

Basically I want URL like this

www.mywebsite.com/{category}/{category1}/{category2}/{category3}/{product}/

No fixed root pointer...any ideas?

I have temporarily fixed it with a route /catalog/ but it is not wanted  

www.mywebsite.com/catalog/{category}/{category1}/{category2}/{category3}/{product}/

IS this possible in ASP.NET or is the global.asax a necessary evil?

Hi,

manga75

I have temporarily fixed it with a route /catalog/ but it is not wanted  

www.mywebsite.com/catalog/{category}/{category1}/{category2}/{category3}/{product}/

IS this possible in ASP.NET or is the global.asax a necessary evil?

 

Please check the sample below,hope this helps.

http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

 

thanks but for if the application was running in MVC this would never have been a problem...it is webforms as posted in the question.

Hi,

manga75

...it is webforms as posted in the question.

 

Please try this:

Using Routing With WebForms

ASP.NET 4.0 WebForms Routing

How to: Use Routing with Web Forms

Hope this helps.

Thank you but I allready know the basics what Im looking for is how to generate the routes automatically from the database so I dont have to register them manually so it work truly like a datadriven site.

I have a database categories table that has a parent child relation so I can build and unlimited amount of subcategories

id (int)
categoryname (varchar)
active (bit)
rawurl (varchar) 
parent int (fk id)

id category active rawurl parent
1 sport TRUE /1/ null
2 winter TRUE /1/2/ 1
3 hockey TRUE /1/2/3/ 2
4 skates TRUE /1/2/3/4/ 3
5 Junior TRUE /1/2/3/4/5/ 4
6 Pro TRUE /1/2/3/4/6/ 4
7 Practise TRUE /1/2/3/4/6/7/ 6

I allready generate the treeview and store it in cache and thats basically what I want to be able to do with the routing aswell so the routing can reflect the database.

<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;" id="_mcePaste">
id category active rawurl parent
1 sport TRUE /1/ null
2 winter TRUE /1/2/ 1
3 hockey TRUE /1/2/3/ 2
4 skates TRUE /1/2/3/4/ 3
5 Junior TRUE /1/2/3/4/5/ 4
6 Pro TRUE /1/2/3/4/6/ 4
7 Practise TRUE /1/2/3/4/6/7/ 6
</div>