Click here to Skip to main content
15,919,500 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i want to insert data using form. i also got error in connectivity

Code posted by OP in comment:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Hospital_Management
{
    public partial class admin_doctor_home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btn_submit_click(object sender, EventArgs e)
        {
            SqlConnection sc = new SqlConnection("Data Source=.\\sqlexpress;initial Catalog=user_master; Integrated Security=True;Pooling=False");
        
            SqlCommand cmd;
            cmd = new SqlCommand();
            cmd.CommandText = "insert into user_master values(" + Txt1.Text + "," + Txt2.Text + "," + TextBox1.Text + "," + TextBox2.Text + "," + TextBox3.Text + "," + DropDownList1.Text + "," + DropDownList2.Text + "," + email_id.Text + "," + no.Text + ")";
            //cmd.CommandType = CodeConstructType.text;
            cmd.Connection = sc;
        
            Response.Redirect("admin_doctor_home.aspx");
        }
    }
}
Posted
Updated 9-Mar-14 3:08am
v3
Comments
Kornfeld Eliyahu Peter 9-Mar-14 8:33am    
Have you done anything so far? Show some effort (code or searching)! As is it ain't a question...
Member 10651903 9-Mar-14 8:35am    
yes i do some effort

// Edit: moved code to the question.
Kornfeld Eliyahu Peter 9-Mar-14 8:37am    
Great! Now do us some favor improve your question by adding your code...(and not just a comment on it)
Member 10651903 9-Mar-14 8:43am    
?
Member 10651903 9-Mar-14 8:46am    
this code is ok?

If it's and "error in connectivity", then try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

Then, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you've done that, always list the fields you want to insert into in order - that way any DB changes don't break your code:
SQL
INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)

And finally, perhaps if you opened the connect, and executed the query, the INSERT might work a bit better?
C#
sc.Open();
cmd.ExecuteNonQuery();
would probably do it...
 
Share this answer
 
Comments
Member 10651903 9-Mar-14 9:10am    
ok thanks
OriginalGriff 9-Mar-14 9:16am    
You're welcome!
Try below connection string

C#
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;


SqlCommand cmd=new SqlCommand();
cmd.Connection= <connection object="">;
cmd.CommandText = <insert sql="" statement="">;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery(); 



Try the above code.

code block added
 
Share this answer
 
v3
Comments
Nelek 9-Mar-14 10:59am    
If you post code in a solution, don't forget to use the "code" tags, if not it might happen that some of the code is interpreted as HTML-Tags and therefore not shown in the solution.

I have added the code tags in your message
Hi,
First of all u i think u have not opened the connection to the database.So 1st open the connection.

sc.open();
cmd.Connection=sc;
cmd.ExecuteNonQuery();

Hopefully this would work.If not working fine pleas let me know about your error.
 
Share this answer
 
Comments
Member 10651903 9-Mar-14 13:38pm    
ok i will do this thanks
This will be a simpler way to do it

C#
string connString = "Data Source=\sqlexpress;initial Catalog=user_master; Integrated Security=True;Pooling=False";
SqlConnection connStudent = new SqlConnection(connString);
connStudent.Open();


C#
string sqlInsertStatement "insert into user_master values ('" Txt1.Text.ToString() + "'" + Txt2.Text.ToString() + "'" + TextBox2.Text.ToString()+ "'" +")";



//insert your other statements there.
//make sure to add .ToString so that it can capture the value inside your textbox

C#
SqlCommand cmdTxt = new SqlCommand(sqlInsertStatement, connStudent);


C#
cmdTxt.ExecuteNonQuery();
connStudent.Close();
connStudent.Dispose();
Response.Redirect("admin_doctor_home.aspx");
 
Share this answer
 
VB
Partial Public Class _Default
    Inherits System.Web.UI.Page
    Public oCn As New System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=MyDatabase;Uid=sa")

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

    Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As String = ""
        If oCn.State = ConnectionState.Closed Then
            oCn.Open()
        End If

        str = "Insert Into mst_employees(name,gender,position) values('" & Me.TextBox1.Text & "','" & Me.TextBox2.Text & "','" & Me.TextBox3.Text & "')"
        Dim cmd As New SqlClient.SqlCommand(str, oCn)
        cmd.ExecuteNonQuery()
        oCn.Close()
        fill_data()
    End Sub

    Sub fill_data()
        If oCn.State = ConnectionState.Closed Then
            oCn.Open()
        End If

        Dim cmd As New SqlClient.SqlCommand("select * from mst_employees", oCn)
        Dim da As New SqlClient.SqlDataAdapter(cmd)
        Dim ds As New DataSet("bpl")
        Dim i As Integer = 0

        da.Fill(ds, "bpl")
        Me.GridView1.DataSource = ds.Tables(0)
        Me.GridView1.DataBind()
        oCn.Close()
    End Sub
End Class
 
Share this answer
 
v2
Comments
Member 10651903 9-Mar-14 8:55am    
thank u

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900