We made a simple form in Visual Studio 2010 to submit data into SQL Server 2008R2 database tables however we can't seem to get it to post the data into the tables. Here is the code we are using;
Thanks in advance, we are new to coding so all help appreciated
PAGE CODE
<h3>Create New Case</h3>
<asp:Table ID="Table1" runat="server" HorizontalAlign="Left" Width="600px">
<asp:TableRow>
<asp:TableCell>Company:</asp:TableCell>
<asp:TableCell><asp:DropDownList ID="Company" runat="server" DataSourceID="SqlDataSource1" DataTextField="client_id" DataValueField="client_id"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:triageConnectionString %>" SelectCommand="SELECT [client_id] FROM [clients] ORDER BY [client_id]"></asp:SqlDataSource></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Requestor:</asp:TableCell>
<asp:TableCell><asp:DropDownList ID="Requestor" runat="server" DataSourceID="SqlDataSource2" DataTextField="contact_id" DataValueField="contact_id"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:triageConnectionString %>" SelectCommand="SELECT [contact_id] FROM [contacts] ORDER BY [contact_id]"></asp:SqlDataSource>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Type:</asp:TableCell>
<asp:TableCell><asp:DropDownList ID="Type" runat="server">
<asp:ListItem>EMR/PM</asp:ListItem>
<asp:ListItem>Software</asp:ListItem>
<asp:ListItem>Hardware Issue</asp:ListItem>
<asp:ListItem>Informative</asp:ListItem>
<asp:ListItem>Installation</asp:ListItem>
<asp:ListItem>Interface</asp:ListItem>
<asp:ListItem>Maintenance</asp:ListItem>
<asp:ListItem>Phone</asp:ListItem>
<asp:ListItem>Research</asp:ListItem>
<asp:ListItem>User Related</asp:ListItem>
<asp:ListItem>Vendor Related</asp:ListItem>
<asp:ListItem>Web Related</asp:ListItem>
<asp:ListItem>GloStream</asp:ListItem>
<asp:ListItem>Server</asp:ListItem>
</asp:DropDownList></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Status:</asp:TableCell>
<asp:TableCell><asp:DropDownList ID="Status" runat="server">
<asp:ListItem>New</asp:ListItem>
<asp:ListItem>Open</asp:ListItem>
<asp:ListItem>Closed</asp:ListItem>
<asp:ListItem>Waiting on Client Response</asp:ListItem>
<asp:ListItem>Esc to Vendor</asp:ListItem>
<asp:ListItem>Esc to Development</asp:ListItem>
<asp:ListItem>Tier 1 Assigned</asp:ListItem>
<asp:ListItem>Tier 1 Processing</asp:ListItem>
<asp:ListItem>Tier 2 Assigned</asp:ListItem>
<asp:ListItem>Tier 2 Processing</asp:ListItem>
<asp:ListItem>Tier 3 Assigned</asp:ListItem>
<asp:ListItem>Tier 3 Processing</asp:ListItem>
<asp:ListItem>Tier 4 Assigned</asp:ListItem>
<asp:ListItem>Tier 4 Processing</asp:ListItem>
</asp:DropDownList></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Priority:</asp:TableCell>
<asp:TableCell><asp:DropDownList ID="Priority" runat="server">
<asp:ListItem>Low</asp:ListItem>
<asp:ListItem>Medium</asp:ListItem>
<asp:ListItem>High</asp:ListItem>
<asp:ListItem>Urgent</asp:ListItem>
</asp:DropDownList></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Summary:</asp:TableCell>
<asp:TableCell><asp:TextBox ID="Summary" runat="server" TextMode="MultiLine"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Description:</asp:TableCell>
<asp:TableCell><asp:TextBox ID="Description" runat="server" TextMode="MultiLine"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell></asp:TableCell>
<asp:TableCell><asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" /></asp:TableCell>
<asp:TableCell></asp:TableCell>
</asp:TableRow>
</asp:Table>
CS Code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class images_newcase : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string company = Company.Text;
string requestor = Requestor.Text;
string type = Type.Text;
string status = Status.Text;
string priority = Priority.Text;
string summary = Summary.Text;
string description = Description.Text;
string connString = ConfigurationManager.ConnectionStrings["triageConnectionString"].ConnectionString;
SqlConnection conn = null;
try
{
conn = new SqlConnection(connString);
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO dbo.cases(name) Values (@var)";
cmd.Parameters.AddWithValue("@var", requestor);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected == 1)
{
//Success notification
}
else
{
//Error notification
}
}
}
catch (Exception ex)
{
//log error
//display friendly error to user
}
finally
{
if (conn != null)
{
//cleanup connection i.e close
}
}
}
}