c# - Connecting Two SQL Server Tables -


i'm trying connect following 2 tables together:

create table [dbo].[groups]  (     [group_id]   int identity (1, 1) not null,     [group_name] nvarchar (50) null,     [group_desc] nvarchar (50) null,      primary key clustered ([group_id] asc) );  create table [dbo].[events]  (     [event_id] int identity (1, 1) not null,     [event_group_id] int null,     [event_type]  nvarchar (50) null,     [event_title] nvarchar (50) null,      primary key clustered ([event_id] asc),      constraint fk_event_group           foreign key (event_group_id) references groups (group_id) ); 

first question: have created tables correctly?

i've been able add events following code haven't been able connect pk of groups fk in events.

does have suggestions?

  sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["ballinoradbconnectionstring1"].connectionstring);         conn.open();         string findgroupofevent = "select group_id groups group_name ='" + dropdownlisteventgroup.selecteditem.tostring() + "'";         int groupid = convert.toint32(findgroupofevent);         string insertquery = "insert events (event_group_id, event_type, event_title) values (@groupeventid, @type, @title)";         sqlcommand com = new sqlcommand(insertquery, conn);         com.parameters.addwithvalue("@groupeventid", groupid);         com.parameters.addwithvalue("@type", dropdownlisteventtype.selecteditem.tostring());         com.parameters.addwithvalue("@title", textboxet.text);         com.executenonquery();         response.redirect("viewevents.aspx");         response.write("registration successful");         conn.close(); 

ui

adding dropdown items

change value in designer screen shot https://i.stack.imgur.com/c0ryz.png 1 , 2 etc corresponds ids used in database table. why code failing cannot convert string name integer. value unique id (int), name display text.

a better , more maintainable option dynamically generate list items database values when form/control loads. there many asked questions answers on databind asp.net list of listitem dropdownlist issue


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -