0

Hi,

I am creating a web application and i am using sqlserver 2005 as back end. I have a page with a radiobuttonlist, a textbox and a submit button. The user needs to select one option and enters values in the textbox. Once he submits the appliation need to insert values in anyone of the five tables based on the option selected in the radio button list. Everything is working fine except i am unable to sqlcommand object. please find the below coding i used for reference.

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 
 
 
public partial class popup : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
 
    } 
    protected void bttnInsertRecord_Click1(object sender, ImageClickEventArgs e) 
    { 
        int i; 
        string sql; 
        SqlConnection conn = new SqlConnection("Data Source=MILESTONE_TECH\MILESTONE_SQL;Initial Catalog=UKGas;UserID=sa;password=sql"); 
        switch (RadioButtonList1.SelectedItem.Value) 
        { 
            case "Vehicle": 
                sql = "insert into Vehicle(RegistrationNumber) values ('"+TextBox1.Text+"')"; 
                break; 
            case "Model": 
                sql = "insert into Model(ModelNumber) values ('" + TextBox1.Text + "')"; 
                break; 
            case "Owner": 
                sql = "insert into Owner(FirstName) values ('"+TextBox1.Text+"')"; 
                break; 
            case "Engine": 
                sql = "insert into Engine(Engine Number) values ('"+TextBox1.Text+"')"; 
                break; 
            case "Repair Courses": 
                sql = "insert into Repair(Repair Course) values ('"+TextBox1.Text+"')"; 
                break; 
            default: 
                TextBox1.Text = " "; 
                break; 
        } 
        conn.Open(); 
        SqlCommand cmd = new SqlCommand(sql, conn); 
        i = cmd.ExecuteNonQuery(); 
        if(i>0) 
        { 
            Label1.Text = "Item Inserted"; 
        } 
    } 
}

All i need is if the user selects option 1 from the radio button then the appropraiate insert query. Can you please help me in this request. Its my college project.

Thanks in advance

3
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by crishlay
0

Are you getting an error at the point of executing the query? Setting the SQL statement by the radio buttons is easy enough, I can't see what you're doing wrong there. You're not closing your connection when you're done either by the way:)

0

use CommandType Text and try below code
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
i = cmd.ExecuteNonQuery();

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.